Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi Guys, I was searched many sites in google.But i doesn't got the solution.

This is my GridView:

<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>
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 15-Nov-12 21:22pm
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add
CommandArgument 
along with
<asp:TemplateField HeaderText="Approve" HeaderStyle-ForeColor="Blue">
            <ItemTemplate>
                <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you only need to find the index then this is how you can do it:
 
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
<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...
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100