Asp.net Grid view Row command Event


Grid View Row Command Event occurs when we click on a button  in Grid View Control. 
It can be used to get the selected row value or  text.

Following is an Example

gridpage.aspx
PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="WebApplication9.WebForm1"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <title></title> </head> <body> <formid="form1"runat="server"> <div> <asp:gridviewID="gv"runat="server"onrowcommand="gv_RowCommand" BackColor="#DEBA84"BorderColor="#DEBA84"BorderStyle="None"BorderWidth="1px" CellPadding="3"CellSpacing="2"> <Columns> <asp:TemplateFieldHeaderText="Select"> <ItemTemplate> <asp:ButtonID="Button1"runat="server"Text="Show"CommandName="Show"/> <!--if you want to use command argument property uncomment the below codeand comment the above code --> <%--<asp:ButtonID="Button1" runat="server" Text="Show"CommandName="Show" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'/>--%> </ItemTemplate> </asp:TemplateField> <asp:TemplateFieldHeaderText="Display"> <ItemTemplate> <asp:ButtonID="Button2"runat="server"Text="Display"CommandName="Display"/> <!--if you want to use command argument property uncomment the below codeand comment the above code --> <%--<asp:Button ID="Button2" runat="server"Text="Display" CommandName="Display" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'/>--%> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyleBackColor="#F7DFB5"ForeColor="#8C4510"/> <HeaderStyleBackColor="#A55129"Font-Bold="True"ForeColor="White"/> <PagerStyleForeColor="#8C4510"HorizontalAlign="Center"/> <RowStyleBackColor="#FFF7E7"ForeColor="#8C4510"/> <SelectedRowStyleBackColor="#738A9C"Font-Bold="True"ForeColor="White"/> <SortedAscendingCellStyleBackColor="#FFF1D4"/> <SortedAscendingHeaderStyleBackColor="#B95C30"/> <SortedDescendingCellStyleBackColor="#F1E5CE"/> <SortedDescendingHeaderStyleBackColor="#93451F"/> </asp:gridview> <br/> <asp:LabelID="Label1"runat="server"Text="Label"></asp:Label> </div> </form> </body> </html>


gridpage.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { fillGrid(); } } public void fillGrid() { DataTable dt = new DataTable(); dt.Columns.Add("EmpNo"); dt.Columns.Add("EmpName"); dt.Columns.Add("Location"); dt.Columns.Add("Designation"); for (int i = 0; i < 5; i++) { DataRow dr = dt.NewRow(); dr["EmpNo"] = "EmpNo-" + i; dr["EmpName"] = "EmpName-" + i; dr["Location"] = "Location-" + i; dr["Designation"] = "Designation-" + i; dt.Rows.Add(dr); dt.AcceptChanges(); } gv.DataSource = dt; gv.DataBind(); } protected void gv_RowCommand(object sender, GridViewCommandEventArgs e) { // The GridViewCommandEventArgs class does not contain a property that indicates which row's command button was // clicked. To identify which row's button was clicked, //use the CommandSource property and cast it's NamingContainer to the GridViewRow. Then you can use it's RowIndex property: GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer; int rowIndex = gvr.RowIndex; //or //if you want to use the command argument property set the command argument property in source code and comment above code and uncomment bellow code //int rowIndex = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "Show") { Label1.Text = gv.Rows[rowIndex].Cells[2].Text; } else if (e.CommandName == "Display") { Label1.Text = gv.Rows[rowIndex].Cells[3].Text; } } }


OUTPUT