Changing Gridview row background color when editing?
I'm having some trouble with editing a gridview row's background color when Editing it.
The thing is, I am using the RowDataBound
event method to change e.Row.BackColor
based on a criteria when displaying the report( 3 different colors depending on result ). Fo开发者_Go百科r the rows that don't fall under that criteria, a GridView's property <EditRowStyle BackColor="#999999" />
is applied upon clicking the Edit button.
However, I can't seem to find a way to change the color of those that do fall under the criteria since RowDataBound seems to be called all the time, overriding any changes I make.
Any suggestions?
write a single line in Grid RowEditing Event:
GridView1.EditRowStyle.BackColor = System.Drawing.Color.LightYellow;
Hope this helps. Configure GridView row editing. This should be enough information. Let me know if you need some more.
protected void uxGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
ClearBackColor();
GridViewRow row = uxGrid.Rows[e.NewEditIndex];
row.BackColor = Color.LightYellow;
}
private void ClearBackColor()
{
foreach (GridViewRow row in uxGrid.Rows)
{
row.BackColor = System.Drawing.Color.Transparent;
}
}
Try:
<asp:GridView runat="server" >
<Columns>
</Columns>
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#999999" />
</asp:GridView>
Why not write you own logic method to change rows back color?, loop through rows, this you can avoid the postback issue...maybe!
As @Rami said create a method that loops through the DataGrid rows and change the color. Call this method in PreRender event handler. This way you are calling this method once every postback.
精彩评论