using a datagridview n.
Skip this Video
Loading SlideShow in 5 Seconds..
Using a DataGridView PowerPoint Presentation
Download Presentation
Using a DataGridView

Loading in 2 Seconds...

play fullscreen
1 / 45

Using a DataGridView - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Using a DataGridView' - giolla

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
the 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
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.
fill in search criteria1
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
View Source

Delete everything except the schedule.

schedule in html1
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
Data File
  • The data file can be downloaded from the class web site:
    • schedule_2011_spring.csv
  • Download the file to a convenient folder if you have not created it from the web site.
design the program
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
The Form

Add a DataGridView control to the form.

Set its name to dgvSchedule.

Anchor on all sides.

implement the program
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
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

automatic properties
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 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
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;



Continued on next slide

the constructor1
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
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 schedule1
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()





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);



import schedule1

// Read the schedule file.

while ((input_line = Reader.ReadLine()) != null)


Schedule_Record sr = new Schedule_Record(input_line);

if (!sr.error_detected)





setting the datasource
Setting the DataSource
  • Finally, we need to make the List be the DataSource for the DataGridView:

dgvSchedule.DataSource = Schedule;

program in action
Program in Action

Scroll right.

useless columns
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
Removing Items from the Grid
  • We can remove the useless columns from the DataGridView by making the corresponding properties private.
error detected issue
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 action1
Program in Action

Note that you can resize the columns.

setting the column widths
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 widths1
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
  • Display the properties of dgvSchedule.
  • Locate AutoSizeColumnsMode
  • Set it to "AllCells"
avoid horizontal scrolling
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
Tighten Up Some Columns

public Form1()




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


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