Menu ▼



Posted By :
avatarsunitha
Total Views : 32541

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


usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Data;


publicpartialclassWebForm1: System.Web.UI.Page

{

protectedvoidPage_Load(objectsender, EventArgse)

{

if(!IsPostBack)

{

fillGrid();

}

}

 publicvoidfillGrid()

 {

 DataTabledt = newDataTable();

 dt.Columns.Add("EmpNo");

 dt.Columns.Add("EmpName");

 dt.Columns.Add("Location");

 dt.Columns.Add("Designation");

 for(inti = 0; i < 5; i++)

  {

  DataRowdr = 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();

}


protectedvoidgv_RowCommand(objectsender, GridViewCommandEventArgs e)

 {

 //The GridViewCommandEventArgs class does not contain a property  thatindicates which row's command button was

 //clicked. To identify which row's button was clicked,

 //usethe CommandSource property and cast it's NamingContainer to  theGridViewRow. Then you can use it's RowIndex property:


 GridViewRowgvr = (GridViewRow)  ((Control)e.CommandSource).NamingContainer;

 introwIndex = gvr.RowIndex;

 //or

 //ifyou want to use the command argument property set the command  argument property in source code and comment above code and  uncommentbellow code

 //introwIndex = Convert.ToInt32(e.CommandArgument);

 if(e.CommandName == "Show")

 {

 Label1.Text= gv.Rows[rowIndex].Cells[2].Text;

 }

 elseif(e.CommandName == "Display")

 {

 Label1.Text= gv.Rows[rowIndex].Cells[3].Text;

 }

 }

 }

 

OUTPUT











comments powered by Disqus


Search dotnetlearners.com