Thursday, October 28, 2010

Set DataRow Values and Other ASP.NET GridView Tips

You can set DataRow values in two ways:
First, using a column name:
myDataRow["City"]= "London";

Or, using an index:
myDataRow[2] = "London";

Don't use ItemArray property to set values, it won't work.

Set Text Box Width in a GridView Bound Field:

Use ControlStyle properties:

<asp:BoundField... ControlStyle-Width="60px"/>

Change GridViewRow Appearance: 

There are different ways to change GridViewRow appearance. The approach I like involves using the GridViewRow Style property. On RowDataBound event use this:

if (e.Row.RowType == DataControlRowType.Footer)
 {                              
     e.Row.Style.Add("color", "#999999");
     e.Row.Style.Add("font-weight", "bold"); 
     ...

Did you know that you could pass data format string as a parameter to the ToString() method?

For example:
e.Row.Cells[1].Text = iTotal.ToString("F2")
The above format string specifies 2 decimal places in a numeric value.

No comments:

Post a Comment