Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CommandArgument in the Gridview

I have a gridview like this.

<asp:GridView ID="grdMerchant" runat="server" GridLines="None"
   HeaderStyle-Font-Bold="True" AutoGenerateColumns="False" AllowSorting="True" ShowHeader="false" OnRowDataBound="grdMerchant_RowDataBound" OnRowCommand="grdMerchant_RowCommand" DataKeyNames="OrderID" style="table-layout:auto;width:100%;" >  


<asp:TemplateField >
<ItemTemplate>
   <asp:Linkbutton ID= "btnView" runat="server" Text="View" OnClick="btnView_OnClick" CommandArgument='<%#Eval("OrderID")%>' ></asp:Linkbutton>

How do i have to get the OrderID of the selected row. I tried using

int OrderID = (int)grdMerchant.DataKeys[row.RowIndex][2];

But it gets null and i know this is not the way. Help me.

Thank you in advance!

like image 210
challengeAccepted Avatar asked Dec 13 '11 18:12

challengeAccepted


People also ask

What is CommandArgument in asp net?

The CommandArgument property complements the CommandName property by allowing you to provide any additional information about the command to perform. For example, you can set the CommandName property to Sort and set the CommandArgument property to Ascending to specify a command to sort in ascending order.

What is RowCommand in GridView C#?

The RowCommand Event occurs when a button is clicked in a GridView control. The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.

How do I get GridView data in RowCommand event?

The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is determined. The TextBox control is referenced using the FindControl method of the GridView Row by passing the ID of the control as parameter.

How do you pass more than one value in CommandArgument?

Multiple values will be passed by concatenating multiple values using a Character separator such as Comma, Pipe, etc. and later inside the Button Click event, the values will be split and populated into an Array.


1 Answers

try like this

 <asp:GridView ID="grd1" runat="Server" width="500px" AutoGenerateColumns="false" DataKeyNames="StateID" OnRowEditing="grd1_RowEditing">
  <Columns>
       <asp:TemplateField>
              <ItemTemplate>
                      <asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%#Eval("StateID")%>' OnCommand="lnkDelete" Text="Delete">
                      </asp:LinkButton>
               </ItemTemplate>
        </asp:TemplateField>
   </Columns>

 protected void lnkDelete(Object sender, CommandEventArgs e)
 {
    int iStID=int32.Parse(e.CommandArgument.ToString());
 }

 //iStID has the DataKey value which you can use. 
like image 105
Glory Raj Avatar answered Oct 12 '22 13:10

Glory Raj