私は選択を有効にしてGridViewを持っています。 GridViewの一部の列には、CssClassが設定され、背景色が設定されます。また、GridViewにはSelectedRowStyleが定義されています。SelectedRowStyle定義されたCSSスタイルのセルのBgColorを変更しない
問題は、行が選択された後、SelectedRowStyleで定義されているように背景色が変更されますが、独自のCssClassが設定されている列は例外です。それらの背景色は変更されず、選択された行のすべての列が同じ色(SelectedRowStyleで定義されている)になります。
私はVS2008と.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;
}
コード:
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()));
}
}