Search code examples
asp.netsortingaspxgridview

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


I have a GridView through which I am displaying some books details and it can be filtered based on book attributes. The problem is this if I filter some books and try to edit one of them the editor pick the id of overall row value not the new one. var id = Convert.ToInt32(gridview.Rows[e.NewEditIndex].Cells[1].Text); I am getting index value through this

 <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>

Solution

  • Try finding control is easier then finding cells because cell is created if you put any container before the control.

    You can try FindControl rather than reading the cell as below how to find control in edit item template?

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

    and cast it accordingly.
    Also if it is a primary key put into data keys of gridview and get it from there. Example Get DataKey values in GridView RowCommand

    Also hopefully you are properly binding data to gridview.