In applications, you often need to modify the presentation of the DataGrid after binding, such
(1) When the data meets certain conditions, you need to modify its rendering mode.
(2) convert the logical value (0, 1) in the database into men and women for presentation.
There are roughly two ways to implement the above operations
(1) modify the value in itemdatabound
(2) Use a function for consideration during binding.
Similarly, databases follow http://www.cnblogs.com/lovecherry/archive/2005/03/25/125487.html
We can implement the following two functions:
(1) In itemdatabound, change the background color of a person younger than 30 to pink.
Code As follows:
Private void maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem | E. Item. itemtype = listitemtype. edititem)
{
If (convert. toint16 (databinder. eval (E. Item. dataitem, "iage") <30) E. Item. backcolor = color. Pink;
}
}
Of course, you need to register the event
This. Maid + = new system. Web. UI. webcontrols. Maid (this. Maid );
(2) display the name of a person named "Zhu Xi" with an increase
Front-end code
<% # Databinder. eval (container. dataitem, "vname") %>
Change
<% # Myfunc (databinder. eval (container. dataitem, "vname") %>
Background code
Protected string myfunc (Object S)
{
String _ s;
If (S. tostring () = "")
_ S = "<B>" + S. tostring () + "</B> ";
Else
_ S = S. tostring ();
Return _ s;
}
The above two features, that is, there are roughly two ways to implement the above operations.
That's simple.
I will not elaborate on other related extensions.
For example, you can change the attributes of some controls based on a value:
If the control is in the template column, it can be dynamically set through (control type) E. Item. findcontrol (ID). Attribute = value;
If the control is not in the template column, you can use (control type) E. Item. controls [index number]). Property = value to dynamically set
For example, the statistical values of other columns are the same according to the values of one column.
Operate in the itemdatabound event method, because the data is bound at this time.