(Original) How to dynamically change the content in the DataGrid after binding

Source: Internet
Author: User

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.


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.