views:

554

answers:

1

I've got GridView with selection enabled. Some columns in GridView have CssClass set, setting their background color. Also, there is SelectedRowStyle defined in GridView.

Problem is, after row is selected, it's background color is changed as defined in SelectedRowStyle, except columns with own CssClass set. Their background color remains unchanged, and I would like all columns in selected row to have same color (defined in SelectedRowStyle).

I'm using VS2008 and .NET Framework 3.5.

GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
OnRowDataBound="GridView1_RowDataBound" AllowPaging="True" PageSize="50" CssClass="gv" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnPageIndexChanged="GridView1_PageIndexChanged" 
DataKeyNames="Name" RowStyle-Wrap="False">
<RowStyle Font-Names="Calibri" Font-Size="Small" BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#336699" Font-Bold="True" ForeColor="White" />
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
    <asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
    <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
    <asp:BoundField DataField="Level" HeaderText="Level" SortExpression="Level" ItemStyle-CssClass="bglightred" />
    <asp:BoundField DataField="Log" HeaderText="Log" SortExpression="Log" ItemStyle-CssClass="bglightred" />
</Columns>

CSS:

    .gv td.bglightred 
{
    background-color: #FF8080;
}

.gv tr:hover td 
{
    background-color: #CCCCCC;cursor: default;
}

Code:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString()));
    }
}
A: 

Well, I solved this problem. In SelectedIndexChanging event of GridView I loop through custom colored cells, remove CSS class definition and restore it after next row is selected. It's somewhat brute-force programming and, honestly, it looks ridiculous to me, but it works. Here is the code:

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
    int i = 0;
    for (i = 4; i <= 5; i++)
    {
        GridView1.Rows[e.NewSelectedIndex].Cells[i].CssClass
= null;
    }
    if (Session["LO_Index"] != null)
    {
        if (Session["LO_Index"].ToString() != "-1")
        {
            int index = Convert.ToInt32(Session["LO_Index"].ToString());
            for (i = 4; i <= 5; i++)
            {
                GridView1.Rows[index].Cells[i].CssClass
= "bglightred";
            }
        }
    } }

LO_Index keeps index of currently selected row, I used it for other purposes in code and here it just came handy.

Anyway, if you know more elegant solution I would be glad to try it.

Igor Drincic