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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With