using a datagridview

45
1 Using a DataGridView

Upload: giolla

Post on 21-Jan-2016

67 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Using a DataGridView

1

Using a DataGridView

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

Page 3: Using a DataGridView

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.

Page 4: Using a DataGridView

4

Where is the schedule?

Ask google!

Page 5: Using a DataGridView

5

Where is the schedule?

Click here

Page 6: Using a DataGridView

6

Where is the schedule?

Click here

Page 7: Using a DataGridView

7

Fill in Search Criteria

Page 8: Using a DataGridView

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)

Page 9: Using a DataGridView

9

The Schedule

Page 10: Using a DataGridView

10

View Source

Delete everything except the schedule.

Page 11: Using a DataGridView

11

Schedule in HTML

Page 12: Using a DataGridView

12

End of the Schedule

Page 13: Using a DataGridView

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.

Page 14: Using a DataGridView

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.

Page 15: Using a DataGridView

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.

Page 16: Using a DataGridView

16

New Project

Page 17: Using a DataGridView

17

Add Class

Page 18: Using a DataGridView

18

The Form

Add a DataGridView control to the form.

Set its name to dgvSchedule.

Anchor on all sides.

Page 19: Using a DataGridView

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

Page 20: Using a DataGridView

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

Page 21: Using a DataGridView

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

}

}

Page 22: Using a DataGridView

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.

Page 23: Using a DataGridView

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

Page 24: Using a DataGridView

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;

Page 25: Using a DataGridView

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.

Page 26: Using a DataGridView

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

}

Page 27: Using a DataGridView

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;

}

Page 28: Using a DataGridView

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

}

}

Page 29: Using a DataGridView

29

Setting the DataSource

Finally, we need to make the List be the DataSource for the DataGridView:

dgvSchedule.DataSource = Schedule;

Page 30: Using a DataGridView

30

Setting the DataSource

Build and run.

Page 31: Using a DataGridView

31

Program in Action

Scroll right.

Page 32: Using a DataGridView

32

After Scrolling Right

Page 33: Using a DataGridView

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

Page 34: Using a DataGridView

34

Removing Items from the Grid

We can remove the useless columns from the DataGridView by making the corresponding properties private.

Page 35: Using a DataGridView

35

Removing Items from the Grid

Page 36: Using a DataGridView

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.

Page 37: Using a DataGridView

37

Program in Action

Note that you can resize the columns.

Page 38: Using a DataGridView

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.

Page 39: Using a DataGridView

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

Page 40: Using a DataGridView

40

AutoSizeColumnsMode

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

Page 41: Using a DataGridView

41

Program Running

Page 42: Using a DataGridView

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

Page 43: Using a DataGridView

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

}

Page 44: Using a DataGridView

44

Final Result

Page 45: Using a DataGridView

45

Summary

The DataGridView control makes it easy to display tabular data.

Properties permit us to control its appearance.