当复选框被点击时如何获取GridView行索引

3

我有一个简单的网格视图。每一行都有一些绑定字段和一个复选框。复选框具有AutoPostBack=True并使用OnCheckedChanged事件触发一个方法(chkPlanned_OnCheckedChanged)。

     <asp:GridView ID="gvAudits" runat="server" EnablePersistedSelection="true"
            AllowSorting="True" CssClass="DDGridView"
            RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6" AutoGenerateColumns="False" DataKeyNames="AuditId" OnSorting="GridView1_Sorting" OnRowDataBound="gvAudits_RowDataBound" OnRowCommand="gvAudits_RowCommand"
            OnDataBound="OnDataBound">

            <Columns>



                <asp:BoundField DataField="AuditDate" SortExpression="AuditDate" DataFormatString="{0:dd/MM/yyyy}" HeaderText="Audit Date" ItemStyle-Width="50px" />

                <asp:TemplateField HeaderText="Division Owner" SortExpression="DivisionOwner" ItemStyle-Width="140px">
                    <ItemTemplate>
                        <div style="width: 140px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
                            <%# Eval("DivisionOwner") %>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Main Auditee" SortExpression="MainAuditee" ItemStyle-Width="140px">
                    <ItemTemplate>
                        <div style="width: 140px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
                            <%# Eval("MainAuditee") %>
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="TotalFindings" SortExpression="TotalFindings" HtmlEncode="False" HeaderText="Total <br />Findings " ItemStyle-Width="25px">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>

                <asp:BoundField DataField="NoMajorFindings" SortExpression="NoMajorFindings" HtmlEncode="False" HeaderText="Major <br />Findings " ItemStyle-Width="25px">
                    <ItemStyle HorizontalAlign="Right"></ItemStyle>
                </asp:BoundField>




                <asp:TemplateField HeaderText="Potential" SortExpression="Planned">
                    <ItemTemplate>
                        <asp:CheckBox runat="server" ID="chkPlanned" Checked='<%# Convert.ToBoolean(Eval("Planned"))%>' AutoPostBack="True"   OnCheckedChanged="chkPlanned_OnCheckedChanged" />
                    </ItemTemplate>
                    <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                </asp:TemplateField>


                <asp:BoundField DataField="AuditId" HeaderText="AuditId" SortExpression="AuditId"
                    ReadOnly="True" Visible="False">
                    <HeaderStyle Width="50px" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        &nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                            OnClientClick='return confirm("Are you sure you want to delete this item?");' CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'><i class="fa fa-remove"></i></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>


            <EmptyDataTemplate>
                There are currently no items in this table.
            </EmptyDataTemplate>
        </asp:GridView>

在C#代码后端中,此方法使用以下逻辑获取rowIndex:
protected void chkPlanned_OnCheckedChanged(object sender, EventArgs e)
    {
  CheckBox chk = (CheckBox) sender;
  GridViewRow gr = (GridViewRow) chk.Parent.Parent;
  var dataKey = gvAudits.DataKeys[gr.RowIndex];
}

一切运作良好,但是我需要添加分组列标题,我通过编程实现如下:

    protected void OnDataBound(object sender, EventArgs e)
    {
        GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
        TableHeaderCell cell = new TableHeaderCell();
        cell.Text = "";
        cell.ColumnSpan = 5;
        row.Controls.Add(cell);

        cell = new TableHeaderCell();
        cell.ColumnSpan = 3;
        cell.Text = "Finding Status";
        row.Controls.Add(cell);

        cell = new TableHeaderCell();
        cell.ColumnSpan = 3;
        cell.Text = "Finding Type";
        row.Controls.Add(cell);


        cell = new TableHeaderCell();
        cell.ColumnSpan = 3;
        cell.Text = "";
        row.Controls.Add(cell);

        row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
        gvAudits.HeaderRow.Parent.Controls.AddAt(0, row);
    }

问题是,现在在添加分组标题后,当我点击复选框时,无法从代码后端获取行索引。gr.RowIndex 总是返回0。有任何想法为什么会这样吗?
也许有一种方法可以在 ASPX 中添加网格视图标题的分组,而不是在代码后面,这样就可以解决问题了? Header with groupings added

请检查我提供的答案。 - Shirish
始终使用 chk.NamingContainer 而不是 chk.Parent.Parent - Tim Schmelter
@DeadlyDee找到了解决方案... - Shirish
1个回答

3
请使用以下代码,希望它能解决你的问题。
public void Chacked_Changed(object sender, EventArgs e)
{
GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
string id = grd1.DataKeys[Row.RowIndex].Value.ToString();
string cellvalue = Row.Cells[1].Text;
}

或使用

 CheckBox chk = (CheckBox)sender; 
 GridViewRow gvr = (GridViewRow)chk.NamingContainer;
 string id = GridView1.Rows[gvr.RowIndex].Value.ToString();
 string cellvalue = GridView1.Rows[gvr.RowIndex].Cells[1].Text;

嗨,这个解决方案不起作用,它仍然给我一个RowIndex = 0。 - DeadlyDan
@DeadlyDee 如果您选择第一行,则行索引将为0...如果您只想要行索引,则可以轻松地通过以下代码获取-> int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex; - Shirish
不行-我也试过这个,但是它不起作用。如果我移除分组标题,一切都正常工作。但是我需要 - DeadlyDan
@DeadlyDee,我已经检查过了,我可以看到在你的rowbound中有一行代码 -> gvAudits.HeaderRow.Parent.Controls.AddAt(0, row); -- 为什么你要在每一行绑定头部控件?我认为这行代码引起了问题。 - Shirish
嗨,标题只绑定了一次,正如图像所示,分组标题插入在主标题之上。就我所见,实现是正确的,我没有发现任何错误?谢谢你的检查 :) - DeadlyDan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接