如何在过滤后获取 GridView 的选定单元格值。 ASP.Net

How to get selected cell value of a GridView after filtration. ASP.Net

我有一个 GridView,我通过它显示一些书籍的详细信息,并且可以根据书籍属性对其进行过滤。问题是,如果我过滤一些书籍并尝试编辑其中一本书,编辑器会选择整体行值的 id 而不是新的。 var id = Convert.ToInt32(gridview.Rows[e.NewEditIndex].Cells[1].Text); 我通过这个获取索引值

 <div class="col">
            <asp:GridView OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView3_RowUpdating" OnRowCancelingEdit="GridView3_RowCancelingEdit" 
                         class="table table-striped table-bordered" ID="GridView3" runat="server" AutoGenerateColumns="false" DataKeyNames="Id" 
              
                           HeaderStyle-BackColor="#95daa4" HeaderStyle-ForeColor="Black" >
                <Columns >
                    <asp:CommandField ShowEditButton="true" ControlStyle-Width="26px" ControlStyle-CssClass="linkGVE"/>
                    <asp:BoundField  DataField="Id"  HeaderText="ID" ReadOnly="True" SortExpression="Id">
                        <ControlStyle Font-Bold="True" CssClass="linkGVE"/>
                        <ItemStyle Font-Bold="True"/>
                    </asp:BoundField>
                    <asp:TemplateField >
                        <ItemTemplate>
                            <div class="container-fluid">
                                    <div class="col-lg-10">
                                        <div class="row">
                                            <div class="col-12">
                                                <asp:Label ID="Label1"  runat="server" class="animated bounceInRight"  Text='<%# Eval("BookName") %>' Font-Bold="True" Font-Size="18px"></asp:Label>
                                            </div>
                                        </div>
                                        <br/>
                                        <div class="row">
                                            <div class="col-12">
                                                <span>
                                                    <b>Province -</b><span>&nbsp;</span>
                                                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("Province") %>'></asp:Label>
                                                </span>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-12">
                                                <b>Subject - </b>
                                                <asp:Label ID="Label5" runat="server" Text='<%# Eval("Subject") %>'> </b></asp:Label>
                                                &nbsp;| <b>Categories -</b>
                                                <asp:Label ID="Label6" runat="server" Text='<%# Eval("Categorey") %>'></asp:Label>
                                                &nbsp;| <b>PackageName -</b>
                                                <asp:Label ID="Label7" runat="server" Text='<%# Eval("PackageName") %>'></asp:Label>

                                            </div>
                                        </div>

                                        <div class="row">
                                            <div class="col-12">
                                                <b>CategoricalTitle -</b>
                                                <asp:Label ID="Label12" runat="server" Text='<%# Eval("CategoricalTitle") %>'></asp:Label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-lg-2 " style="margin-top: 25px; text-align: center;">

                                        <asp:Image class="img-fluid" BorderWidth="2px"  BorderColor="#82aeb1"   ID="Image1" Height="100px" Width="120px" runat="server" ImageUrl='<%# "http://0.0.0.0/Pictures/" + Eval("PictureURL") %>'/>
                                        <a href='<%# "http://0.0.0.0.0/PDF/" + Eval("PDFURL") %>' style="font-size: 11px;color: black">Download</a>
                                    </div>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

尝试查找控件比查找单元格更容易,因为如果您将任何容器放在控件之前,就会创建单元格。

您可以尝试 FindControl 而不是阅读下面的单元格 how to find control in edit item template?

GridView1.Rows[e.NewEditIndex].FindControl("control");

并相应地进行投射。
此外,如果它是放入 gridviewdata keys 中的主键并从那里获取它。示例 Get DataKey values in GridView RowCommand

也希望您正确地将数据绑定到 gridview。

我没有在页面重新加载时应用 ispostback 条件。一旦我应用了条件,我的代码就开始工作。谢谢