Friends of the C # language must be more or less exposed to WinForm programming, and using the designer in the visual IDE of the C # language (such as vs.net) allows us to easily combine controls such as forms, buttons, labels, picture frames, and so on. We can easily make WinForm applications with friendly interface. We can also make our application more plump by the powerful event handling mechanism in the WinForm program.
Of course, we do not talk about the form this time, do not talk about buttons ...
Let's talk about DataGridView (data grid view).
As the interface that really interacts with the user, it is necessary to show the data to the user in a complete way, and it is used to display the data in the powerful WinForm related controls. A lot, but as the list view shows the controls, push the ListView and DataGridView. When displaying multiple rows and columns of data, they can often be replaced by each other.
Because I think DataGridView interface is not as friendly as the ListView, so is to use the ListView instead of DataGridView, but recently like to use DataGridView, the main reason when it is a powerful data processing.
It can intelligently handle the data source that is bound to it, and it can be a very different type of binding as a data source.
The DataGridView class supports the standard Windows forms data binding model. This means that the data source can be any type that implements one of the following interfaces:
IList interface, including one-dimensional arrays.
IListSource interfaces, for example, DataTable and DataSet classes.
IBindingList interface, for example, BindingList (of T) class.
IBindingListView interface, for example, the BindingSource class.
For details, see http://msdn.microsoft.com/zh-cn/library/ System.windows.forms.datagridview.datasource.aspx (Datagridview.datasource property)
For our usual programming, it is possible to bind the DataTable and list<t> to the DataGridView datasource is more than the case.
But the point I want to say is (personal opinion, not guarantee right, true): If it is a DataTable, it is best to use the DataView of the DataTable; if it is ilist<t>, it is best to use BindingList <T>.
First, let's look at the important attributes in DataGridView:
①autogeneratecolumns Properties (see http://msdn.microsoft.com/zh-cn/library/ system.windows.forms.datagridview.autogeneratecolumns.aspx)
This attribute is important and one of the important reasons: it is not found in the designer properties of VS (I do not know why it is not added to the attributes listed in the property list ...). )。 The function of this property is simple and important: Specifies whether the DataGridView automatically increases the column.
You must set this property to True if you want to specify the properties that will be displayed to the data grid view bound object by manually adding a column;
For example, the following:
I want to display the custom object "employee Information" in the data grid view:
Employee class:
1 using System;
3 namespace Dgveg
4 {
5 //Employee class
6 Public class se:icloneable
7 {
8 //Employee information
9 Public string Name {get; set;}
Ten public int: Age {get; set;}
public int Workno {get; set;}
Public Gender Sex {get; set;}
Public Signrecord Record {get; set;}
/ /Construction method
Public SE () {}
Public SE (int workno, string name, Int. age, Gender Sex)
{
This . Workno = Workno;
. name = name;
This . Age = age;
This . sex = sex;
This . Record = new Signrecord ();
+ }
Enumeration that represents the gender:
1 using System;
3 namespace Dgveg
4 {
5// <summary>
6//Gender
7//</summary>
8 Public enum Gender
9 {
Ten Male = 0, //male
One Female = 1 //female
-- }
13}
Employee's card record (i.e. sign-in check-out record):
1 using System;
3 namespace Dgveg
4 {
5// <summary>
6///clock-out record
7//</summary>
8 Public Class Signrecord
9 {
Time of registration
Public DateTime Signintime {get; set;}
//check-back Time
Public DateTime Signouttime {get; set;}
+ }
15}
The result would be if I directly bound the DataGridView of the Employee object to the datasource of the data source and did not do any filtering on the DataGridView:
There are a few problems, we have to solve one by one, the first is the column name, we certainly want to be Chinese, so we need to manually specify the column name.
If you use VS as a development tool, you can refer to my approach:
Is that all right? We run the program again, but we get the following result:
...... No one wants to do this, so let's go back to the code that binds the data source:
1 //define test data
2 se se1 = new SE (10001, "Test 1th", Gender.female);
3 se se2 = new SE (10002, "Test number 2nd", Gender.male);
4 se se3 = new SE (10003, "test number 3rd", Gender.female);
5 se se4 = new SE (10004, "Test number 4th", Gender.male);
6 se se5 = new SE (10005, "Test 5th", Gender.female);
7 se se6 = new SE (10006, "Test 6th", Gender.male);
9 //Using test data to create a data source
list<se> List = new list<se> ();
list. AddRange (new se[] {SE1, se2, SE3, Se4, SE5, se6});
+ //bound data source
Datagridview1.datasource = new bindinglist<se> (list);
There seems to be no problem, this code can be put into such as the form Load event in the correct execution.
It is not difficult to see from the picture above that the columns we have manually added are intrinsic, but because DataGridView is powerful with the data source, it automatically adds columns to display the data source information in a normal and complete manner.
So we're not going to let it automatically add columns, OK?
Add a code before binding the data source, as follows:
1 //...
2 //...
3//Specify Data grid view does not automatically increment columns
4 datagridview1.autogeneratecolumns = false;
5 //bound data source
6 Datagridview1.datasource = new bindinglist<se> (list);
As expected, the results you see will be:
Please don't worry, take a closer look, the number of items is correct! The data is no problem, the problem is that we want to display the data in our own way, then DataGridView's automatic analysis data will not be executed, we must specify which line to display the object's property. The second important attribute to be followed is the effect:
②datapropertyname Properties (see http://msdn.microsoft.com/zh-cn/library/ system.windows.forms.datagridviewcolumn.datapropertyname.aspx)
It is worth noting that this property is owned by the DataGridView column, not the DataGridView itself. The idea is to specify which of the columns in the DataGridView displays which property of the data source object. You can easily set this property by using the VS designer, such as:
With this set up, we can get the correct result:
Similarly, you can do this when you bind a data table to DataGridView.
Now, we are trying to increase the difficulty and display the attendance information of the employee corresponding to the clock-in record. Many friends who will extrapolate may do the following:
It turns out that this is not going to work ...
This practice is not supported.
In fact, we also hope that we can solve the problem: gender, we certainly hope to show Chinese men and women. How do you do it? There is actually an event that can be done:
③rowsadded event (see http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.rowsadded (VS.80). aspx) (no guarantee of a simpler approach)
This event occurs when one or more rows of data are added to the current DataGridView.
Its usage is as follows:
1//datagridview.rowsadded Event usage
2 private void datagridview1_rowsadded (object sender, DataGridViewRowsAddedEventArgs e)
3 {
4 //String str = "";
5 for (int i = E.rowindex; I < E.rowindex + E.rowcount; i++)
6 {
7 //str + = Datagridview1.rows[i]. CELLS[2]. Value.tostring () + "";
8 }
9 //MessageBox.Show (str);
Ten }
It is worth noting that the event is not added to the DataGridView to be executed once, it is possible to add an item for the first time, add three items, or one item, or all of the rest ...
And it's not the first time you add a second time without adding ...
In short, there is no regularity ...
Then we can pass from E. RowIndex (the index of the first row currently added) to E.rowindex + E.rowcount (the number of items already added) loop, and through the change of E. The value of the rowindex gets the line that is actually added to the operation.
Let's see how we can replace female and male with the corresponding Chinese:
1 private void Datagridview1_rowsadded (object sender, DataGridViewRowsAddedEventArgs e)
2 {
3 //If you can get gender through the object, you can use the following method to display the corresponding Chinese
5 //Cancel binding on column properties
6 column4.datapropertyname = null;
8 for (int i = E.rowindex; I < E.rowindex + E.rowcount; i++)
9 {
by the bound object of the line (or item) to get the gender
Datagridview1.rows[i]. Cells[column4.index]. Value =//Determines how this is displayed, where the Databoungitem is equivalent to the tag being automatically bound when the data source is bound (of course, it is read-only ...). )
(Datagridview1.rows[i]. Databounditem as SE). Sex = = Gender.female? "Female": "Male";
- }
+ }
Good use of control properties and events will make our development more effective!
DataGridView Analysis of WinForm programming Data View