devilking
devilking

Reputation: 79

Without using SelectedIndexChanged, how to retrieve value of each row from gridview?

I have a GridView, without using SelectedIndexChanged, how can I retrieve the value of each row from GridView when click on each button in each row?

this is my aspx code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
        DataSourceID="SqlDataSource1" ShowHeader="False" AllowPaging="True" BorderColor="White"
        CellPadding="6" GridLines="None" Height="100px" Width="800px">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    Card Name:
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                    <br />
                    Cost :
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                    <br />
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductImgID") %>'></asp:Label>
                    <asp:Image ID="Image3" runat="server" ImageUrl='<%# Eval("ProductImgUrl", "images/{0}") %>' />
                    <br />
                    <asp:Button ID="btnAddProduct" runat="server" Text="Add" />
                    <br />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Upvotes: 0

Views: 1964

Answers (4)

Jithu
Jithu

Reputation: 11

        <asp:GridView ID="grdResults" CssClass="CommonTable dataTable" AutoGenerateColumns="false"    runat="server">
                                                    <Columns>
                                    <asp:TemplateField HeaderText="Sl#">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSlno" Text='<%# Container.DataItemIndex+1 %>' runat="server"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderText="" ControlStyle-Height="15px" ControlStyle-Width="15px">
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Left" CssClass="PInfoTd" />
                                        <ItemTemplate>
                                            <asp:ImageButton ID="lknassesno" ToolTip="Edit Assessment" Width="50" CssClass="NewButton" ***CommandName="LINK"***
                                                runat="server" ImageUrl="~/img/Edit.png" />
                                            <asp:HiddenField ID="hidassesmentno" Value='<%#  EVAL("PAN_CODE")%>' runat="server" />
                                            <asp:HiddenField ID="hidPendStatus" Value='<%#  EVAL("Pstatus")%>' runat="server" />
                                            <asp:HiddenField ID="hidIPNO"Value='<%#EVAL("IP_NO")%>' runat="server" />

                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>



**code behind**
    Protected Sub grdResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdResults.RowCommand
            If **e.CommandName = "LINK"** Then
                Dim ctrl As Control = e.CommandSource
                Dim grrow As GridViewRow = ctrl.Parent.NamingContainer
                'Dim i As Integer = Convert.ToInt16(e.CommandArgument)
                'Dim lknassesno As HiddenField = DirectCast(e.CommandSource, ImageButton)
                Dim hidAssesmentNo As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidassesmentno"), HiddenField)
                Dim lblstatus As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidPendStatus"), HiddenField)
                Dim hidIpNo As HiddenField = DirectCast(grdResults.Rows(grrow.RowIndex).FindControl("hidIPNO"), HiddenField)
                Dim Assno As String = hidAssesmentNo.Value
                Dim Ipno As String = hidIpNo.Value
                Dim st As String = ""
                If lblstatus.Value = "Pending" Then
                    st = "E"`enter code here`
                ElseIf lblstatus.Value = "Completed" Then
                    st = "V"
                End If
                Response.Redirect("Assessment.aspx?PAssNo=" & Assno & "&Mode=" & st & "&IPNO=" & Ipno & "")
            End If
        End Sub

Upvotes: 0

Nalaka526
Nalaka526

Reputation: 11464

Add CommandArgument='<%# Container.DataItemIndex %>' to your Add button

<asp:Button ID="btnAddProduct" runat="server" Text="Add" CommandArgument='<%# Container.DataItemIndex %>'/>

To retrive Name, in gridview row command use this code

Dim gvr As GridViewRow = grvGRNCONs.Rows(e.CommandArgument)
Dim name As String = DirectCast(gvr.FindControl("Label1"), Label).Text

and so on..

Upvotes: 0

Tanzeel ur Rehman
Tanzeel ur Rehman

Reputation: 429

To get a row value, you have to get the row Reference, After getting the row you can easily get to the specified column and can get the value

Lets Consider you have a "link button control" in a template field column. For the gridview you have to set Row Command Event, and also in the edit template of the column, set a command name for the link button also say "lnkTest"

In RowCommand Event you have to include the following section of code

if(e.CommandName.Equals("lnkTest")) // Checks that link button was clicked

  { 
    GridViewRow grdRow = (((LinkButton)e.CommandSource).Container)

// This Will give you the reference of the Row in which the link button was clicked

    int grdRowIndex = grdRow.RowIndex;

//This will give you the index of the row

    var uniqueDataKeyValue = GridView1.DataKeys[grdRowIndex].Value;

//This will give you the DataKey Value for the Row in which the link Control was click

Hope the above code will help

Upvotes: 0

Mubarek
Mubarek

Reputation: 2689

One option can be to Bind the CommandArgument of the button to the ProductID

<asp:Button ID="btnAddProduct" runat="server" Text="Add" CommandArgument='<%#Bind("ProductID")%>' />

and then in the RowCommand event retrieve the ProductID and extract the row from database

string prodID=(string)e.CommandArgument()

then using the ID retrieve the row from database.

Upvotes: 2

Related Questions