Dynamic addition of Data columns in the GridView, binding method (1/2)

Source: Internet
Author: User

Dynamic addition of Data columns in the gridview. Binding method

Add the expected column by inheriting the gridview and reloading createcolumns (), and generate the column based on the key-value pairs in the extended attribute.
By inheriting bouldfield and reloading initializedatacell () and getvalue (), you can bind your desired method to the method-based data binding, this allows you to bind the key values in the extended attributes to the corresponding cell.

The gridview is an asp tutorial. one of the table data display controls in. net, which supports data binding. We generally use an object that implements the ienumerable <t> interface for the bound data source, t can be any clr class (of course there are some other data source formats). These are familiar to everyone, but recently we have encountered a new requirement:

For example:

Public class book
{
Public int id {get; set ;}

Public string name {get; set ;}

Public dictionary <string, object> extendproperties {get; set ;}
}

 

Extendproperties are used to store custom extended attributes of different users. They are saved in the form of key-value pairs, the key stores the extended attribute names, and the value stores the extended attribute values, each user may have different numbers of extended attributes and key values.

Now, if we need to bind list <book> to the gridview, but want to display all the extended attributes in extendproperties, and the corresponding key is displayed in the column header, the value is displayed in the corresponding cell.

At first, I wanted to follow the idea of binding general data to the gridview to check whether the key-value pairs in extendproperties can be matched to the corresponding object property through dynamic attributes (no test was conducted to determine whether the data was successful ), however, we feel that data binding is a matter of control. This should not be the responsibility of the data itself. can we achieve this by modifying the behavior of the gridview itself ?? The answer is yes!

As we all know, the gridview itself has an autogeneratecolumns attribute. If it is set to true, the gridview will automatically add some columns, which correspond to each attribute that can be bound in the book, this is implemented using propertydescriptor (it seems that the design mode is taken into account, and the type is not used for reflection ). Based on this idea, we can first look at the effects of autogeneratecolumns on the gridview and how the gridview generates columns.

By viewing the source code of the gridview, we can see several important methods:

Code

Protected virtual icollection createcolumns (pageddatasource datasource, bool usedatasource );

Protected virtual autogeneratedfield createautogeneratedcolumn (autogeneratedfieldproperties fieldproperties );
 

The createcolumns method is used by the gridview to generate columns, in which the autogeneratecolumns attribute is used to determine whether to call createautogeneratecolumn to automatically generate columns with corresponding attributes. Both methods can be reloaded. Based on our needs, we can build a subclass of the gridview, reload the createcolumns method, and call the base. insert the column we want to generate in the returned results of createcolumns.

The Code is as follows:

Code

# Region properties

Public bool autogenerateextendpropertiescolumn {get; set ;}

Public int autogeneratecolumnfter {get; set ;}

Public string extendpropertiesdatafield {get; set ;}

# Endregion

Protected override icollection createcolumns (pageddatasource datasource, bool usedatasource)
{
Icollection collection = base. createcolumns (datasource, usedatasource );

If (autogenerateextendpropertiescolumn)
{
Arraylist list = new arraylist ();

Icollection extendpropertiescollection = createextendpropertiescolumns (datasource, usedatasource );

Arraylist list1 = new arraylist ();
Foreach (var c in collection)
{
List1.add (c );
}

Arraylist list2 = new arraylist ();
If (extendpropertiescollection! = Null)
{
Foreach (var c in extendpropertiescollection)
{
List2.add (c );
}
}

Int copyfrom = autogeneratecolumnfter <list1.count? Autogeneratecolumnfter: list1.count-1;
Copyfrom = copyfrom> = 0? Copyfrom:-1;

For (int I = 0; I <= copyfrom; I ++)
{
List. add (list1 [I]);
}

List. addrange (list2.toarray ());

For (int I = copyfrom + 1; I <list1.count; I ++)
{
List. add (list1 [I]);
}
Return list;
}
Return collection;
}

Protected virtual icollection createextendpropertiescolumns (pageddatasource datasource, bool usedatasource );

 

 

Here, I added several attributes and a new method:

Code
Public bool autogenerateextendpropertiescolumn {get; set ;}

Public int autogeneratecolumnfter {get; set ;}
 
Public string extendpropertiesdatafield {get; set ;}

Protected virtual icollection createextendpropertiescolumns (pageddatasource datasource, bool usedatasource );
 

Autogenerateextendpropertiescolumn is used to determine whether to generate the column corresponding to the extended attribute,

While autogeneratecolumnsafter is used to determine where the automatically generated column should be inserted,

Extendpropertiesdatafield is used to specify which attribute stores the information of the extended attribute in the bound object,

The createextendpropertiescolumns method is used to generate columns corresponding to extendproperties.

Here is just a simple example. How to add columns dynamically by reloading the createcolumns method.

1 2

Related Article

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.