Geeks With Blogs
Carmel's Blog Independent Software Professional

There are a lot of cases we need to add new button column in our DataGridView so we can perform custom logic for each row. For instance add to shopping card a product by pressing on ADD button in the DataGridView.

One way to perform this task is to add template column to the DataGridView that contains a button bounded to the Product ID Field and Command Event:

1. Add Template column: clip_image002

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. Enter to Edit Mode of the new Template Column and add command button:

clip_image004:

3. Bind the button CommandArgument Property to the product ID:

clip_image006

4. Set the command property of the button. e.g. “AddToBasket”.

5. in the server side you catch the command in one of two events:

a. Button command event:

protected void Button_Command(object sender, CommandEventArgs e)
{
        switch (e.CommandName)
        {
            case "ADDToBasket":
                // get the product id from e.CommandArgument
                break;

            default:
                break;
        }
}
b. DataGridView Row command event:
protected void Row_Command(object sender, GridViewCommandEventArgs e)
{
   switch (e.CommandName)
   {
          case "ADDToBasket":
               // get the product id from e.CommandArgument
              break;

          default:
             break;
     }
}
Posted on Tuesday, November 11, 2008 5:44 AM | Back to top


Comments on this post: Adding Button Column to DataGridView

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Carmel Halabe | Powered by: GeeksWithBlogs.net