Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Guys, I was searched many sites in google.But i doesn't got the solution.

This is my GridView:

XML
<asp:GridView 
        ID="gvwEditRequisition" 
        runat="server" 
        AutoGenerateColumns="False"
        CellPadding="3" Style="table-layout: auto; width: 1000px;" Height="240px"    
        OnRowCommand="gvwEditRequisition_RowCommand"
        AllowPaging="True" AllowSorting="True" 
        OnPageIndexChanging="gvwEditRequisition_PageIndexChanging"
        OnSorting="gvwEditRequisition_Sorting" BackColor="White" 
        BorderColor="#CCCCCC"
        BorderStyle="None" BorderWidth="1px">
        <Columns>
            <asp:TemplateField HeaderText="Approve" HeaderStyle-ForeColor="Blue">
                <ItemTemplate>
                    <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:LinkButton ID="lbnREQNO" runat="server" Text="REQNO" CommandArgument="REQNO"
                        CommandName="Sort"></asp:LinkButton>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblREQNO" runat="server" Text='<%# Eval("REQNO") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

C#
protected void gvwEditRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Approve")
    {
        int index =  Int32.Parse(e.CommandArgument.ToString());  // here I got the error is : Input string was not in a correct format.
        Label lblReqNo = gvwEditRequisition.Rows[index].FindControl("lblREQNO") as Label;
        string s =Convert.ToString( lblReqNo);
        string ss = s;
        Response.Redirect("UpdateRequisition.aspx");
    }
}

I want ReqNO into String.

please any one help me.....
Posted
v7

Add
C#
CommandArgument 

along with
XML
<asp:TemplateField HeaderText="Approve" HeaderStyle-ForeColor="Blue">
            <ItemTemplate>
                <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
 
Share this answer
 
If you only need to find the index then this is how you can do it:

C#
LinkButton l = e.CommandSource as LinkButton; 
if(l != null)
{
 GridViewRow selectedRow = l.NamingContainer as GridViewRow ;
 if(selectedRow != null)
 {
  int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);
 }
}


Its a little crude way of doing it but it works.
 
Share this answer
 
You are getting this error because you don't have not specified any CommandArgument in the Approve LinkButton, but trying to access it.

So, if you want to get the index of the GridView row from which LinkButton is clicked then you need to add that row index in the CommandArgument property like below.
XML
<ItemTemplate>
     <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve" CommandArgument="<%#((GridViewRow)Container).RowIndex%>">
     </asp:LinkButton>
</ItemTemplate>

As we have given the rowindex to the CommandArgument now, so when you execute the code, you will get the index of the row from which the button is clicked.

Refer Get RowIndex In GridView RowCommand Event Using DataKey[^] for more details.

Thanks...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900