Analysis on the data view of winform Programming

Source: Internet
Author: User

Those who are studying C # must have more or less access to winform programming. In visual ide of C # Language (such as. net) the designer allows us to easily complete the combination of forms, buttons, labels, image boxes, and other controls. We can easily make a winform application with user-friendly interfaces. We can also use the powerful event processing mechanism in the winform program to make our applications more plump.

Of course, we will not talk about forms or buttons this time ......

Let's talk about the datagridview (Data Grid view ).

As an interface for real information interaction with users, data needs to be fully presented to users, while winform-related powerful controls are used for data display. A large number of widgets are displayed in the List View. When displaying data in multiple rows and columns, the two can often be replaced with each other.

The author thinks that the interface of the dview is not as friendly as that of the listview, so we try to use listview instead of the datagridview, but recently we like to use the datagridview, mainly because of its powerful data processing.

It can intelligently process the bound data sources, and can be used as a data source to bind a large number of types.

The datagridview class supports the standard Windows form data binding model. This means that the data source can be any type that implements one of the following interfaces:
Ilist interface, including a one-dimensional array.
Ilistsource interface, such as datatable and dataset class.
Ibindinglist interface, for example, bindinglist (of T) class.
Ibindinglistview interface, for example, bindingsource class.

For more information, see http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.datasource.aspx (datagridview. datasource properties)

For our usual programming, there may be many cases where datatable and list <t> are bound to the datasource of the datagridview.

But what I want to talk about is (personal opinion, not sure, true): If it is a able, it is best to use dataview OF THE datatable; if it is an ilist <t>, it is best to use bindinglist <t>.

First, let's take a look at the important attributes in the datagridview:

① Autogeneratecolumns properties (see http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.autogeneratecolumns.aspx)

This attribute is very important. One of the important reasons is that it cannot be found in the designer attribute of vs (I don't know why it cannot be added to the attribute list ......). The role of this attribute is very simple and important: specify whether the datagridview automatically adds columns.

If you want to manually add a column to specify the property of the bound object to be displayed in the Data Grid view, you must set this property to true;

For example:

I want to display the custom object "employee information" in the Data Grid View ":

Employees:

1 using system;
2
3 namespace dgveg
4 {
5 // employees
6 public class SE: icloneable
7 {
8 // employee information
9 Public string name {Get; set ;}
10 public int age {Get; set ;}
11 Public int workno {Get; set ;}
12 public gender sex {Get; set ;}
13 public signrecord record {Get; set ;}
14
15 // Constructor
16 public Se (){}
17
18 public Se (INT workno, string name, int age, gender sex)
19 {
20 This. workno = workno;
21 This. Name = Name;
22 This. Age = age;
23 This. Sex = sex;
24 This. Record = new signrecord ();
25}

Gender enumeration:

 

1 using system;
2
3 namespace dgveg
4 {
5 /// <summary>
6 // gender
7 /// </Summary>
8 Public Enum gender
9 {
10 male = 0, // male
11 female = 1 // female
12}
13}

Staff card Recording (that is, sign-in and sign-out records ):

1 using system;
2
3 namespace dgveg
4 {
5 /// <summary>
6 // punch-in records
7 /// </Summary>
8 public class signrecord
9 {
10 // sign-in time
11 Public datetime signintime {Get; set ;}
12 // return time
13 public datetime signouttime {Get; set ;}
14}
15}

If I directly bind the collection block of the "Employee object" as the data source to the datasource OF THE datagridview, and do not perform any filtering operations on the datagridview, the result will be as follows:

  

We can solve several problems one by one. The first is the column name. We certainly want it to be Chinese. In this case, we need to manually specify the column name.

If you use vs as a development tool, you can refer to my practices:

  

Is that all right? We run the program again, but we get the following results:

  

...... It is estimated that no one is willing to do this. Let's return to the Code for binding the Data source:

1 // define test data
2 se SE1 = new Se (10001, "Test 1", 19, Gender. Female );
3 Se se2 = new Se (10002, "test No. 2", 20, Gender. Male );
4 se se3 = new Se (10003, "test No. 3", 21, Gender. Female );
5 se Se4 = new Se (10004, "test No. 4", 22, Gender. Male );
6 se se5 = new Se (10005, "test No. 5", 23, Gender. Female );
7 se se6 = new Se (10006, "Test 6", 24, Gender. Male );
8
9 // use test data to create a data source
10 list <Se> List = new list <Se> ();
11 List. addrange (New se [] {SE1, se2, se3, Se4, se5, se6 });
12
13 // bind the data source
14 datagridview1.datasource = new bindinglist <Se> (list );

It seems that there is no problem. This code can be correctly executed in a form loading event, for example.

From the above picture, it is not difficult to see that the columns we manually add are inherent, but because of the strong processing of the data source, the datagridview automatically adds columns to display the data source information normally and completely.

So we won't allow it to automatically add columns. Isn't that OK?

Add the following code before binding the Data source:

1 //......
2 //......
3 // The specified data grid view does not automatically add columns
4 datagridview1.autogeneratecolumns = false;
5 // bind the data source
6 datagridview1.datasource = new bindinglist <Se> (list );

As expected, the result is:

  

Please do not worry. Take a closer look. The number of items is correct! It indicates that the data is okay. If we want to display the data in our own way, the automatic analysis data of the datagridview will not be executed, we must specify the attribute of the row to display the object. The second important attribute is as follows:

② Datapropertyname property (see http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewcolumn.datapropertyname.aspx)

It is worth noting that this attribute is owned by the column of the datagridview, not the datagridview itself. Therefore, it is used to specify which attribute of the data source object is displayed in the column of the datagridview. You can easily set this attribute through the vs designer, such:

  

We can get the correct result through settings as shown in the following figure:

  

Similarly, you can bind a data table to a dview.

Now, we try to increase the difficulty and display the information of the employee's check-in record. Many of you may perform the following operations:

  

It turns out that this is not the case ......

This approach is not supported.

We also hope to solve another problem: gender, we certainly hope to show Chinese male and female. How can this problem be solved? In fact, there is an event that can be done:

③ Rowsadded event (see http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridview.rowsadded (vs.80). aspx) (not guaranteed to have a simpler approach)

This event occurs when one or more rows of data are added to the current dview.

Its usage is as follows:

1 // use the datagridview. rowsadded event
2 Private void maid (Object sender, maid E)
3 {
4 // string STR = "";
5 For (INT I = E. rowindex; I <E. rowindex + E. rowcount; I ++)
6 {
7 // STR + = maid [I]. cells [2]. value. tostring () + "";
8}
9 // MessageBox. Show (STR );
10}

It is worth noting that this event is not executed once to add a row to the datagridview. It is possible to add one item for the first time, add three items for the second time, or add one or all other items ......

And it's not the first time you 've added it for the second time ......

In short, there is no law ......

Then we can. rowindex (index of the first row currently added) to E. rowindex + E. rowcount (number of items already added) cycle, and through the change of E. the rowindex value is used to obtain the actually added row.

Let's take a look at how we can replace female and male with corresponding Chinese characters:

1 private void maid (Object sender, maid E)
2 {
3 // if you can get the gender through the object, you can use the following method to display the corresponding Chinese
4
5 // unbind column attributes
6 column4.datapropertyname = NULL;
7
8 For (INT I = E. rowindex; I <E. rowindex + E. rowcount; I ++)
9 {
10 // obtain the gender through the bound object of the row (or item)
11 datagridview1.rows [I]. cells [column4.index]. value = // determine how to display the data. The databoungitem here is equivalent to the tag automatically bound when the data source is bound (of course, it is read-only ......)
12 (maid [I]. databounditem as SE). Sex = gender. Female? "Female": "male ";
13}
14}

Being good at using the properties and events of controls will make our development more efficient!

  

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.