Change Rows Background color in GridView with selected Criteria
2011-09-01 11:42
549 查看
In this article we change background color of the rows in a GridView with selected criteria on GridView1_RowDataBound event.
In aspx.cs page
In aspx.cs page write the code for the Gridview1_RowDataBound Event. Set the criteria you want for the Gridview Row Backcolor Change. The GridView offers a RowDataBound event that fires once for each row after the row has been created and bound to the corresponding
record of data from the data source control. In this article I am changing the row background color when CategoryID == 2; you can use whatever criteria you need to.
protected
void Gridview1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType ==
DataControlRowType.DataRow)
{
// determine the value of the UnitsInStock field
int CategoryID =
Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
"CategoryID"));
if (CategoryID == 2)
// color the background of the row yellow
e.Row.BackColor = System.Drawing.Color.Brown;
}
}
In aspx page
In the aspx page on the GridView set the RowDataBound event in the GridView property. Then, in the RowDataBound line, type in the name of the event handler you want to create (I named my event handler productsGridView_RowDataBound). Finally, enter in the following
code into the event handler on OnRowDataBound="Gridview1_RowDataBound" .
Conclusion
In this article we try to improve GridView design based on certain criteria. Hopefully it's useful for you.
In aspx.cs page
In aspx.cs page write the code for the Gridview1_RowDataBound Event. Set the criteria you want for the Gridview Row Backcolor Change. The GridView offers a RowDataBound event that fires once for each row after the row has been created and bound to the corresponding
record of data from the data source control. In this article I am changing the row background color when CategoryID == 2; you can use whatever criteria you need to.
protected
void Gridview1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType ==
DataControlRowType.DataRow)
{
// determine the value of the UnitsInStock field
int CategoryID =
Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,
"CategoryID"));
if (CategoryID == 2)
// color the background of the row yellow
e.Row.BackColor = System.Drawing.Color.Brown;
}
}
In aspx page
In the aspx page on the GridView set the RowDataBound event in the GridView property. Then, in the RowDataBound line, type in the name of the event handler you want to create (I named my event handler productsGridView_RowDataBound). Finally, enter in the following
code into the event handler on OnRowDataBound="Gridview1_RowDataBound" .
Conclusion
In this article we try to improve GridView design based on certain criteria. Hopefully it's useful for you.
相关文章推荐
- How to: Change color of rows in GridView on mouseover
- How to change the text color of the selected property sheet tab in Visual C++
- Getting all selected rows in ADF Table with multiple rows selection enabled
- change background color in chrome
- How to change tooltip background color in Unity?
- how can i change the back color on mouse out to the color specified in gridview
- change the color scheme in vim
- IOS开发入门:在Storyboard中为UITableView中的static cells设置选中背景色selectedBackgroundColor
- How to change background color of gedit embedded terminal
- How to change the default browser to debug with in Visual Studio 2008?
- Assertion failure in -[UICGColor encodeWithCoder:]
- Change background color of a UIAlertView
- Change GridView RowColor OnMouseClick
- How to change the background color of an Edit Box
- Change column color for columnChart in extjs
- reviewboard-使用RBtools工具提交review申请,提示more files in your changeset has history scheduled with commit.
- flex3升级到flex4.1时候遇到的一个问题。Compiler error with selectedChild in Flex 4 with code that worked in Flex 3
- 怎样控制Qt标题栏(How can I handle events in the titlebar and change its color etc ? )
- mySql主从配置出错:The server is not configured as slave; fix in config file or with CHANGE MASTER TO
- Coloring the Console Application (AddingText and Background color in C#)