480 likes | 781 Views
Using a DataGridView. The DataGridView. The DataGridView is a high level control that presents data in a form similar to an Excel spreadsheet. Can be linked to a data source so that it automatically shows whatever is in the source. Example: CSE Schedule.
E N D
The DataGridView • The DataGridView is a high level control that presents data in a form similar to an Excel spreadsheet. • Can be linked to a data source so that it automatically shows whatever is in the source.
Example: CSE Schedule • We will create a csv file with the schedule for CSE department undergraduate courses using the results of a search in OASIS. • Read the file. • Store the schedule information in a data structure in memory. • Display the information in a DataGridView.
Where is the schedule? Ask google!
Where is the schedule? Click here
Where is the schedule? Click here
Fill in Search Criteria • Enter search parameters: • Term: Spring 2011 • Campus: Tampa • Department: Engineering Computer Science • Level: Undergraduate • Status: All • Click Search button (near bottom)
View Source Delete everything except the schedule.
Schedule in HTML • Replace all instances of with space characters. • Save as schedule_2011_spring.html • Double click to open file in browser • Verify that it looks right. • Right click and Open with Excel. • Save as CSV • schedule_2010_spring.csv • Reopen in Excel and Notepad to verify. • Note credit hours for Ind Study.
Data File • The data file can be downloaded from the class web site: • http://www.cse.usf.edu/~turnerr/Software_Systems_Development/Downloads/Schedule_Viewer/ • schedule_2011_spring.csv • Download the file to a convenient folder if you have not created it from the web site.
Design the Program • Create a new Windows forms project. • Define a class to hold a schedule entry. • Class Schedule_Record • Add a DataGridView control to the form. • Resize to occupy most of the screen.
The Form Add a DataGridView control to the form. Set its name to dgvSchedule. Anchor on all sides.
Implement the Program • On Page_Load • Read the file. • Skip over lines that are not schedule entries. • For each schedule entry, create a Schedule_Record object. • Add the object to a List. • Finally, make the List the DataSource for the DataGridView control. • Details on following slides
Class Schedule_Record • The DataGridView will automatically display all public properties of the objects in its data source. • Headings will be the properties’ class names. • In class Schedule_Record create an automatic property for each column in the schedule grid. • Plus one more, error_detected http://www.cse.usf.edu/~turnerr/Software_Systems_Development/Downloads/Schedule_Viewer/Schedule_Record.cs
Automatic Properties class Schedule_Record { public String session { get; set; } public String college { get; set; } public String department { get; set; } public String reference { get; set; } public String course_number { get; set; } public String section { get; set; } public String course_title { get; set; } public String credit_hours { get; set; } public String permit { get; set; } public String status { get; set; } public String seats_open { get; set; } public String days { get; set; } public String time { get; set; } public String building { get; set; } public String room { get; set; } public String instructor { get; set; } public String campus { get; set; } public String delivery_method { get; set; } public String fees { get; set; } public bool error_detected { get; set; } } }
Add Constructor • Add a constructor for Schedule_Record • Take a (csv) string as input. • Split the string, creating an array of strings. • Set the properties from the array.
The Constructor // Constructor public Schedule_Record(String S) { String[] Schedule_Info; Schedule_Info = S.Split(','); if ((Schedule_Info.Length < 19) || (Schedule_Info[1] != "EN")) { error_detected = true; return; } Continued on next slide
The Constructor session = Schedule_Info[0]; college = Schedule_Info[1]; department = Schedule_Info[2]; reference = Schedule_Info[3]; course_number = Schedule_Info[4]; section = Schedule_Info[5]; course_title = Schedule_Info[6]; credit_hours = Schedule_Info[7]; permit = Schedule_Info[8]; status = Schedule_Info[9]; seats_open = Schedule_Info[10]; days = Schedule_Info[11]; time = Schedule_Info[12]; building = Schedule_Info[13]; room = Schedule_Info[14]; instructor = Schedule_Info[15]; campus = Schedule_Info[16]; delivery_method = Schedule_Info[17]; fees = Schedule_Info[18]; error_detected = false;
Importing the Schedule • Now we need to add code to the Page_Load handler to read the file and set up a List of Schedule_Record objects.
Importing the Schedule using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; namespace Schedule_Viewer { public partial class Form1 : Form { List<Schedule_Record> Schedule; public Form1() { InitializeComponent(); import_schedule(); }
import_schedule void import_schedule() { StreamReader Reader = null; String input_line; String file_name; Schedule = new List<Schedule_Record>(); file_name = @"c:\schedule_2011_spring.csv"; Reader = new StreamReader(file_name); if (Reader == null) { MessageBox.Show("Failed to open file " + file_name); return; }
import_schedule // Read the schedule file. while ((input_line = Reader.ReadLine()) != null) { Schedule_Record sr = new Schedule_Record(input_line); if (!sr.error_detected) { Schedule.Add(sr); } }
Setting the DataSource • Finally, we need to make the List be the DataSource for the DataGridView: dgvSchedule.DataSource = Schedule;
Setting the DataSource Build and run.
Program in Action Scroll right.
Useless Columns • Several columns are useless, as every entry has the same value, or they just don’t provide any information: • session • college • department • campus • delivery_method • error_detected
Removing Items from the Grid • We can remove the useless columns from the DataGridView by making the corresponding properties private.
error_detected Issue • We will need an accessor method for error_detected for use by Form1.cs Modify import_schedule to use the function.
Program in Action Note that you can resize the columns.
Setting the Column Widths • We can set the column widths programatically. • The DataGridView has a collection of objects that define the columns. • Columns property • Each object in Columns is of type DataGridViewColumn. • Class DataGridViewColumn has a Width property, which we can set. • Or we can ask the system to set the width automatically.
Setting the Column Widths • How do we determine each column's width? • Alternative methods: • Determine size of strings in the columns. • Cut and try. • Set the DataGridView's AutoSizeColumnsMode
AutoSizeColumnsMode • Display the properties of dgvSchedule. • Locate AutoSizeColumnsMode • Set it to "AllCells"
Avoid Horizontal Scrolling • Let’s try to get an entire schedule record to fit on the screen without scrolling in the horizontal direction. • Make the form and the DataGridView somewhat wider. • Tighten up some columns where the headings are wider than the data. • Section • Credit_Hours • Seats_Open
Tighten Up Some Columns public Form1() { InitializeComponent(); import_schedule(); dgvSchedule.DataSource = Schedule; DataGridViewColumnCollection cols = dgvSchedule.Columns; cols[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; cols[2].Width = 30; // Section cols[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; cols[4].Width = 30; // Credit Hours cols[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; cols[7].Width = 30; // Seats Open }
Summary • The DataGridView control makes it easy to display tabular data. • Properties permit us to control its appearance.