0

我有一个 asp.net 网格视图,我的结构是这样的..

<asp:GridView ID="gridContractor" runat="server" AllowPaging="True" AllowSorting="True"
                AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None" EnableModelValidation="True"
                DataKeyNames="DeviceID" OnRowCommand="gridContractor_RowCommand" OnPageIndexChanging="gridContractor_PageIndexChanging"
                Width="100%" EmptyDataText = "No records to display">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField HeaderText="Device IMEI" DataField="DeviceID" Visible="false">
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="175" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="Person Name" DataField="PersonName">
                        <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="#Observations" DataField="GpsPointsCount" ControlStyle-Width="50px">
                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="50" />
                    </asp:BoundField>
                    <asp:BoundField HeaderText="#Violations" DataField="ViolationCount" ControlStyle-Width="60px">
                        <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" Width="60" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" Text="View" CommandName="View" Enabled="true"
                                CommandArgument="<%#Bind('DeviceID') %>" />
                        </ItemTemplate>
                        <HeaderStyle Width="50" />
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:TemplateField>
                </Columns>
                <RowStyle CssClass="RowStyle" />
                <EmptyDataRowStyle CssClass="EmptyRowStyle" />
                <PagerStyle CssClass="PagerStyle" />
                <SelectedRowStyle CssClass="SelectedRowStyle" />
                <HeaderStyle CssClass="HeaderStyle" />
                <EditRowStyle CssClass="EditRowStyle" />
                <AlternatingRowStyle CssClass="AltRowStyle" />
            </asp:GridView>

即使我指定了<SelectedRowStyle CssClass="SelectedRowStyle">哪个是外部 Css,我也无法看到所选行背景颜色的变化

我也尝试过这样做<SelectedRowStyle BackColor="AliceBlue" />,但仍然没有任何变化,有人能告诉我为什么这不起作用吗?

4

1 回答 1

0

从您发布的代码中,您无处选择一行。如果你想SelectedRowStyle应用它,你必须这样做。

有几种方法可以选择一行:

  • 使用按钮/图像/链接CommandName="Select"
  • SelectedIndex通过从后面的代码设置网格视图来手动处理它。

例如,在您的行命令处理程序中gridContractor_RowCommand,您可以编写:

protected void gridContractor_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
    [...]
    GridViewRow selectedRow = (GridViewRow((Button)e.CommandSource).NamingContainer;
    int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);
    gridContractor.SelectedIndex = intRowIndex 
    [...]
}
于 2013-06-20T12:43:39.510 回答