Search
Categories
  ASP.NET
Visual C#
Visual Basic
.NET Framework
ADO.NET
AJAX
LINQ
Java
 
Tutorial Rating
 
3.1 out of 5


Total Votes: 149
         
1 2 3 4 5
 




Using Button columns in GridView

Author: Waqas Anwar - Posted Date: 01-April-2008 - Category: ASP.NET   
Bookmark and Share
GridView control is one of the most powerful controls in ASP.NET 2.0. This control displays database records in typical tabular form, and provides features such as Sorting, Paging, Selection and Editing without writing a single line of code. It also has many different types of fields (columns) such as hyperlinks, images, checkboxes etc.
 
In the following tutorial, I will show you different techniques you can use to display command buttons in GridView.  For this tutorial I am using Microsoft famous sample database Northwind. I am displaying Products table in the GridView. Also make sure you are setting GridView DataKeyFields property to the Primary Key column of the Product Table such as ProductID. GridView control RowCommand event will give you the ProductID of the product from the GridView Rows when user will click any of the button in the GridView.

Here is the code of GridView control.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" BorderColor="#336699"
                BorderStyle="Solid" BorderWidth="2px" CellPadding="4" DataKeyNames="ProductID"
                DataSourceID="SqlDataSource1" ForeColor="#333333">
                
         <Columns>

               <asp:BoundField DataField="ProductID"  HeaderText="ProductID" SortExpression="ProductID" />


               <asp:ButtonField ButtonType="link" CommandName="ProductName" 
                     DataTextField="ProductName" HeaderText="Name" SortExpression="ProductName" />


               <asp:ButtonField ButtonType="button" CommandName="MoreDetail" 
                     HeaderText="More Details" Text="More Details" />


               <asp:ButtonField ButtonType="Link" CommandName="ViewCategory" 
                     HeaderText="View Category" Text="View Category" />


               <asp:ButtonField ButtonType="Image" CommandName="BuyNow" 
                     HeaderText="Buy Now" ImageUrl="buynow.gif" />


               <asp:ButtonField DataTextField="UnitsInStock" HeaderText="Stock" 
                     ButtonType="button" DataTextFormatString="{0} Items" CommandName="Stock" />
                        

         </Columns>
               
</asp:GridView>


GridView Button Fields


To handle click event of the buttons in GridView you need to handle RowCommand event of the GridView control. Following code will show you how you can get RowIndex, ProductID and CommandName of the button when user click any button in the GridView.

VB.NET

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
      Dim currentCommand As String = e.CommandName
      Dim currentRowIndex As Integer = Int32.Parse(e.CommandArgument)
      Dim ProductID As String = GridView1.DataKeys(currentRowIndex).Value

      Label1.Text = "Command: " & currentCommand
      Label2.Text = "Row Index: " & currentRowIndex.ToString
      Label3.Text = "Product ID: " & ProductID
End Sub


C#

protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
   
    string currentCommand = e.CommandName;
    int currentRowIndex = Int32.Parse(e.CommandArgument); 
    string ProductID = GridView1.DataKeys[currentRowIndex].Value;
   
    Label1.Text = "Command: " + currentCommand;
    Label2.Text = "Row Index: " + currentRowIndex.ToString;
    Label3.Text = "Product ID: " + ProductID;
   
}



RELATED TUTORIALS

  Using Hyperlink columns in GridView
  Formatting GridView output based on Data
  Editing Data using ASP.NET GridView Control
  Display Microsoft Excel File in ASP.NET GridView
  Using Checkbox in ASP.NET GridView Control
  Deleting multiple Rows in GridView using Checkbox
  Displaying Total in Footer of GridView
  Display Microsoft Excel File in ASP.NET GridView

LEAVE YOUR COMMENTS LEAVE YOUR COMMENTS
 Name (required)  
 Email (required
 Website


 
POSTED COMMENTS POSTED COMMENTS

Hi You make sure that DataKeyFields property is set as the primary key column of your datasource.
Posted by Waqas Anwar on Thursday, May 01, 2008

The code did not work.. It had some syntax mistakes(The c# one).
I fixed the syntax mistakes and it didnt work still.
The most important thing for me is to get the index of the row on which the button was clicked.
please try to reply.
Posted by Rawad on Thursday, May 08, 2008

I had change the syntax error in c# as below:

int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
string ProductID = GridView1.DataKeys[currentRowIndex].Value.ToString();

If I add in DataKeyNames="ProductID, Name" then how do I get the value of product name (Name)?

Thank you.
Posted by Vincent Tan on Wednesday, May 28, 2008

Everything is perfect except for the usage of ( ) insted of [ ] for the rowindex
Posted by navya on Friday, August 08, 2008

Thanks boss it was very usefull
Posted by Siraj on Monday, August 18, 2008

Excelent, symple and easy
Posted by oscar andres on Tuesday, October 07, 2008

thanks a lot - you have helped me fix a problem that was buggin me!
Posted by simone on Tuesday, October 21, 2008

Hi it works only for my first page, as there are many pages in my gridview so when i click on the others pages it show:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Please help.
Posted by Rebecca on Saturday, December 27, 2008

By the way, im using image button to select the id. Is there a way to click on the imagebutton to retreive the same result as yours without me selecting ont the 'select' button?
Posted by Rebecca on Saturday, December 27, 2008

Nice, thanks. You would not believe how many over-complicated solutions to this problem show up in Google before you get to this perfectly straightforward one!
Posted by Steve on Thursday, January 15, 2009

Presented simply and works!!
Posted by Barbara on Monday, April 27, 2009

what about delete event?
Posted by timber24 on Tuesday, May 12, 2009

Thanks. It was very helpful. After correcting syntax errors, it worked perfectly fine. Thanks again.
Posted by Yogi on Wednesday, December 02, 2009

testing...
Posted by jai on Wednesday, March 31, 2010

Thank u
Posted by jasmin on Saturday, April 24, 2010

thanks! it was extremely useful
Posted by hw on Monday, June 07, 2010

Clean, Precise and good code.
Posted by Atul on Sunday, June 27, 2010

this is really a good site
Posted by Abhilash on Tuesday, July 13, 2010

 


 
 
 

Categories

My Portfolio

Website Links


Copyright @ 2009 EzzyLearning.com