1 / 45

Using a DataGridView

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.

giolla
Download Presentation

Using a DataGridView

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using a DataGridView

  2. 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.

  3. 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.

  4. Where is the schedule? Ask google!

  5. Where is the schedule? Click here

  6. Where is the schedule? Click here

  7. Fill in Search Criteria

  8. 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)

  9. The Schedule

  10. View Source Delete everything except the schedule.

  11. Schedule in HTML

  12. End of the Schedule

  13. 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.

  14. 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.

  15. 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.

  16. New Project

  17. Add Class

  18. The Form Add a DataGridView control to the form. Set its name to dgvSchedule. Anchor on all sides.

  19. 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

  20. 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

  21. 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; } } }

  22. 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.

  23. 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

  24. 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;

  25. 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.

  26. 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(); }

  27. 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; }

  28. 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); } }

  29. Setting the DataSource • Finally, we need to make the List be the DataSource for the DataGridView: dgvSchedule.DataSource = Schedule;

  30. Setting the DataSource Build and run.

  31. Program in Action Scroll right.

  32. After Scrolling Right

  33. 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

  34. Removing Items from the Grid • We can remove the useless columns from the DataGridView by making the corresponding properties private.

  35. Removing Items from the Grid

  36. error_detected Issue • We will need an accessor method for error_detected for use by Form1.cs Modify import_schedule to use the function.

  37. Program in Action Note that you can resize the columns.

  38. 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.

  39. 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

  40. AutoSizeColumnsMode • Display the properties of dgvSchedule. • Locate AutoSizeColumnsMode • Set it to "AllCells"

  41. Program Running

  42. 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

  43. 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 }

  44. Final Result

  45. Summary • The DataGridView control makes it easy to display tabular data. • Properties permit us to control its appearance.

More Related