.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

RowCommand Event in GridView

Posted By:Syed Shakeer Hussain       Posted Date: June 20, 2010    Points: 20    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this Article you will know how to use the RowCommand Evenet in Gridview.
 

RowCommand Event is used to get the selected GridView Row value or text .

GridView.RowCommand Event:-

RowCommand Event Occurs when a button is clicked in a GridView control.

 

<asp:GridView OnRowCommand="GridViewCommandEventHandler" />

 

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.

 

CommandArgument:-

The CommandArgument can contain any string set by the programmer. The CommandArgument property complements the CommandName property by allowing you to provide any additional information for the command. 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

 

 

CommandName:-

CommandName property to determine the command to perform. The CommandArgument property complements the CommandName property by allowing you to provide any additional information for 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.

 

 

 

Below is the Image of GridView after binding the data

In .aspx source code page


    
       
           
 

           
        
           
            

 

  
    
      
            
            

 

    
       
  
       
    
 

 page Binding Data in GridView

protected void Page_Load(object sender, GridViewCommandEventArgs e)

{

OleDbConnection conn_MSACCESS = new OleDbConnection(ConfigurationManager.ConnectionStrings["MS-db1ConnectionString"].ConnectionString);
            conn_MSACCESS.Open();
             OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "vendortbl");
            GridView1.DataSource = ds.Tables["vendortbl"];
            GridView1.DataBind();

}

Below is the code for getting the selected Row in Gridview using RowCommand Event.

 

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "selectproduct")
    {
           textBox1.Text = Convert.ToString(e.CommandArgument .ToString ());
     }
     if (e.CommandName == "selectvendor")
     {
            string vendid = Convert.ToString(e.CommandArgument.ToString());
            Response.Write("Vendorid:" + vendid);
     }
}

 Below is the Image of GridView Seleted Row usign RowCommand Event.


 Subscribe to Articles

     

Further Readings:

Responses
Author: bhanu         Company URL: http://www.dotnetspark.com
Posted Date: November 24, 2010

Iam getting command argument value and it is assigned to textbox when it is debugging but it is not displaying in textbox..

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend