ASP GridView按索引选择行

时间:2014-03-24 06:47:05

标签: c# asp.net gridview

我有GridView元素。我用一些颜色突出显示所选行。但问题是当我选择新元素时,之前选择的元素仍然会突出显示。这里的方法和东西

CS

protected void CompanyGV_SelectedIndexChanged(object sender, EventArgs e)
{

    // CompanyGV.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
    GridViewRow row = CompanyGV.SelectedRow;
    row.BackColor = System.Drawing.ColorTranslator.FromHtml("#BCED91");                   
}

ASPX

<asp:GridView ForeColor="Black"  Height="400px" ID="CompanyGV" ShowHeaderWhenEmpty="true"  runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="companies_id" DataSourceID="SqlDataCompanyList" OnSelectedIndexChanged="CompanyGV_SelectedIndexChanged"> 

                             <Columns>

                                 <asp:CommandField ButtonType="Button" SelectText="Select" ShowSelectButton="True" />

                                <asp:BoundField HeaderText="№" DataField="companies_id" InsertVisible="False" ReadOnly="True" SortExpression="companies_id"/>
                                <asp:BoundField DataField="companies_name" HeaderText="Company" SortExpression="companies_name" />
                            </Columns>

</asp:GridView>

我想限制多个选择,我想只突出显示所选元素!有没有方法可以为整个GridView生成颜色?或者是否有办法逐行选择并遍历行?

我尝试写这样的smth但我收到stack overflow错误消息

 for (int i = 0; i <= CompanyGV.Rows.Count - 1; i++)
 {

      CompanyGV.SelectRow(i);
      CompanyGV.SelectedRow.Cells[i].BackColor =System.Drawing.ColorTranslator.FromHtml("#FFFFFF");
}

2 个答案:

答案 0 :(得分:1)

我更喜欢客户端。试试这段代码

<强> JQuery的

<script src="~/Content/jquery-ui-1.10.4.custom.js"></script>

<script type="text/javascript">
    $(function() {
        $('#CompanyGV tr').click(function () {
            $('#<%=CompanyGV.ClientID%> tr').removeClass("selected");
            $(this).addClass("selected");
        });
    });
</script>

<强> CSS

.selected{
  background-color:Green !important;
}  

答案 1 :(得分:0)

在设计视图中,转到网格视图常见任务,然后单击编辑列,然后使用命令字段节点选择选择列,屏幕截图位于此处:

Image