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_thumb

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

 clip_image004_thumb

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

 clip_image006_thumb

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 6:32 AM | Back to top


Comments on this post: Adding Button Column to DataGridView

# re: Adding Button Column to DataGridView
Requesting Gravatar...
This is great but when I tried, my "field binding" is disable, which means I cannot set the commandArgument to ProductID.

Why ?
Thank you for your help
Left by Jane on Sep 22, 2009 12:38 AM

# re: Adding Button Column to DataGridView
Requesting Gravatar...
Hi gain,

where are trying to configure the field binding?
Left by carmel on Sep 23, 2009 1:44 AM

# re: Adding Button Column to DataGridView
Requesting Gravatar...
//
public DataSet Search_student(string COURSE_APP)
{
conn.Close();
conn.Open();
sqlcmd = new SqlCommand("ADMIN_FUNCTION", conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@CALLTYPE", "SEARCHSTUDENT");
sqlcmd.Parameters.AddWithValue("@SELECTCOURSE", COURSE_APP);
sqladap = new SqlDataAdapter();
sqladap.SelectCommand = sqlcmd;
sqladap.Fill(dataset);
return dataset;

}

//Method will get Minimum Qualification for applied course
public String getMinQual(string coursename)
{
conn.Close();
conn.Open();
sqlcmd = new SqlCommand("ADMIN_FUNCTION", conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@CALLTYPE", "GETMIN");
sqlcmd.Parameters.AddWithValue("@COURSE_NAME",coursename );
//DR = new SqlDataReader();
DR = sqlcmd.ExecuteReader();
if (DR.Read())
return DR.GetString(0).ToString();
else
return null;
}

//Method will filter student and populate it according to search criteria
public DataSet PopulateStudent(string MinQual,String COURSEAPP,int min ,int max)
{
conn.Close();
conn.Open();
sqlcmd = new SqlCommand("ADMIN_FUNCTION", conn);
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@CALLTYPE", "POPULATE");
sqlcmd.Parameters.AddWithValue("@SELECTCOURSE", COURSEAPP);
sqlcm
Left by harsh on Dec 07, 2009 5:17 AM

Your comment:
 (will show your gravatar)


Copyright © Carmel Halabe | Powered by: GeeksWithBlogs.net