簡體   English   中英

單擊復選框時如何獲取GridView RowIndex

[英]How to get GridView RowIndex when checkbox is clicked

我有一個簡單的gridview。 每一行都有一些boundFields和一個復選框。 該復選框具有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);
    }

問題是,現在當我在添加分組標題后單擊一個復選框時,我無法再從代碼隱藏中獲取RowIndex。 gr.RowIndex總是返回0.任何想法為什么會這樣?

也許有一種方法可以在ASPX中向gridview標頭添加分組,而不是在后面的代碼中添加分組,然后這將解決問題? 添加分組的標題

使用下面的代碼,希望它能解決您的問題

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM