Getting values from GridView Bound, Template and DataKeys Columns on RowCommand Event

Filed in Uncategorized Leave a comment

I have a button field on my GridView on the click of that button I need to pick the values for that particular row.

1. First I need to define CommandName for the button in GridView from design view of page.

<asp:ButtonField ButtonType=”Button” Text=”Send” CommandName=”Send” />

2. For writing code I have selected RowCommand Event of the GirdView.

protected void gvnewProRequests_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == “Send”)
int intindex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvrselectedRow = gvnewProRequests.Rows[intindex];

//Form bound columns
int intPOId = Convert.ToInt32(Server.HtmlDecode(gvrselectedRow.Cells[0].Text));
//I am not able to find any solution for getting cell values by name thats why using index

//From DataKeys
int intSellerId = Convert.ToInt32(gvnewProRequests.DataKeys[intindex].Values["SId"]);

//From Template columns Textbox
string strprokNo = ((TextBox)gvrselectedRow.FindControl(“txtgvProNo”)).Text;

DataKeys are very useful as hidden columns you can easily define any columns of your datatable whcih is bind to your GirdView as DataKey column by adding tag DataKeyNames in GridView design mode, you can also define muliple columns as shown below

<asp:GridView runat=”server” ID=”gvnewProRequests” AutoGenerateColumns=”False” OnRowCommand=”gvnewProKitRequests_RowCommand” DataKeyNames=”ProKitStatusId,SId”>