C # WinForm DataGridView let DataPropertyName support complex properties

Source: Internet
Author: User

First, add the BindingSource to the grid, and the type is BINDINGFORFORM2. or set the DataSource of the grid to ienumerable<bindingforform2>.

The BINDINGFORFORM2 types are as follows.

  Public classBindingForForm2 { Public intAge {Get;Set; }  Public stringName {Get;Set; }  Public intHeight {Get;Set; }  Public intWeight {Get;Set; }  PublicClasstest Classtest {Get;Set; } }     Public classClasstest { Public stringS1 {Get;Set; }  Public stringS2 {Get;Set; } }
View Code

We want to display the S1 and S2 properties of the Classtest property in BindingForForm2 directly on the grid. Can be set as DataPropertyName. Set the way you use attribute points directly.

You can then register the DataGridView cellformatting event as follows. The code basically means to take the object of the currently selected line (BindingForForm2 here), then take the DataPropertyName setting, and then loop through the reflection to read the desired value.

1  Private voidDatagridview1_cellformatting (Objectsender, Datagridviewcellformattingeventargs e)2         {3             if(E.value! =NULL)return;4             Try5             {6                 varDGV =(DataGridView) sender;7                 Objectobj =NULL;8                 varSource = DGV. DataSource asBindingSource;9                 if(Source! =NULL)Ten                 { Oneobj = ((ienumerable<Object>) source. DataSource). ElementAt (e.rowindex); A                 } -                 Else if(DGV. DataSource isienumerable<Object>) -                 { theobj = ((ienumerable<Object>) DGV. DataSource). ElementAt (e.rowindex); -                 } -                 varCol =DGV. Columns[e.columnindex]; -                 varprops = Col. Datapropertyname.split ('.'); +                 foreach(varPropinchprops) -                 { +                     if(obj = =NULL)return; A                     varp =obj. GetType (). GetProperty (prop); at                     if(p = =NULL)return; -obj = P.getvalue (obj,NULL); -                 } -E.value =obj; -             } -             Catch in             { -                 //Ignore to             } +}

Effect:

BindingSource Populating data

Bindingforform2bindingsource.datasource =NewList<bindingforform2>            {                NewBindingForForm2 {age =Ten, Height = -, Name ="XXX", classtest =Newclasstest {S1 ="Sdjfkljlk"}},                NewBindingForForm2 {age = A, Height = -, Name ="ASD", classtest =Newclasstest {S2 ="CCCCCCC"}},                NewBindingForForm2 {age = -, Height = -, Name ="ASDD"}            };
View Code

GridView Display

Reprint please indicate the source. Http://www.cnblogs.com/JmlSaul/p/7726233.html

C # WinForm DataGridView let DataPropertyName support complex properties

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.