How to handle DataGridViewButtonColumn click event in C#

By FoxLearn 2/28/2025 9:22:50 AM   124
Handling the DataGridViewButtonColumn click event in C# involves a few key steps.

In this guide, I’ll demonstrate how to handle the button click event for a DataGridViewButtonColumn in a WinForms application.

Steps to Handle DataGridViewButtonColumn Click Event

  1. Set up the DataGridView with a button column.

  2. Bind data to the DataGridView.

  3. Add an event handler for the CellContentClick event.

  4. Check if the button column was clicked.

  5. Execute the button click logic using the bound data.

DataGridView with a Button Column for Product Details

The goal is to display the details of a product when its corresponding button is clicked.

Step 1: Set the DataSource to a BindingList<Product>

First, create a list of Product objects and bind it to the DataGridView as shown below.

var products = new BindingList<Product>()
{
    new Product() { Id = 1, Name = "Laptop", Price = 1200.00m },
    new Product() { Id = 2, Name = "Smartphone", Price = 800.00m },
    new Product() { Id = 3, Name = "Tablet", Price = 500.00m },
    new Product() { Id = 4, Name = "Headphones", Price = 150.00m }
};

dgvProducts.DataSource = products;

Step 2: Define the Click Handler Method

Create a method to handle the button click. This method will display the product’s details in a message box.

public void ShowProductDetails(Product product)
{
    MessageBox.Show($"Product ID: {product.Id}\nName: {product.Name}\nPrice: {product.Price:C}");
}

Step 3: Add a DataGridViewButtonColumn to the DataGridView

Add a button column to the DataGridView and set its Tag property to the ShowProductDetails method.

dgvProducts.Columns.Add(new DataGridViewButtonColumn()
{
    Text = "View Details",
    Tag = (Action<Product>)ShowProductDetails,
    UseColumnTextForButtonValue = true,
    AutoSizeMode = DataGridViewAutoSizeColumnMode.None
});

The Tag property is assigned a delegate pointing to the ShowProductDetails method, which will be used later to invoke the handler.

Step 4: Handle the CellContentClick Event

Wire up the CellContentClick event and implement the handler logic.

First, subscribe to the event:

dgvProducts.CellContentClick += DgvProducts_CellContentClick;

Next, implement the event handler:

private void DgvProducts_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    var grid = (DataGridView)sender;

    if (e.RowIndex < 0)
    {
        // They clicked the header column, do nothing
        return;
    }

    if (grid[e.ColumnIndex, e.RowIndex] is DataGridViewButtonCell)
    {
        var clickHandler = (Action<Product>)grid.Columns[e.ColumnIndex].Tag;
        var product = (Product)grid.Rows[e.RowIndex].DataBoundItem;

        clickHandler(product);
    }
}

In this example:

  • We check if the clicked cell is a DataGridViewButtonCell.

  • We retrieve the ShowProductDetails delegate from the Tag property of the button column.

  • We fetch the bound Product object from the row and pass it to the handler.

Suppose you want to add a second button column to delete a product. You can do this without modifying the existing CellContentClick handler:

Add a new method for deleting a product:

public void DeleteProduct(Product product)
{
    var result = MessageBox.Show($"Are you sure you want to delete {product.Name}?", "Confirm Delete", MessageBoxButtons.YesNo);
    if (result == DialogResult.Yes)
    {
        ((BindingList<Product>)dgvProducts.DataSource).Remove(product);
    }
}

Add a new button column for deletion:

dgvProducts.Columns.Add(new DataGridViewButtonColumn()
{
    Text = "Delete",
    Tag = (Action<Product>)DeleteProduct,
    UseColumnTextForButtonValue = true,
    AutoSizeMode = DataGridViewAutoSizeColumnMode.None
});

The existing CellContentClick handler will automatically work with the new button column because it retrieves the handler from the Tag property.

By following these steps, you can efficiently handle button clicks in a DataGridViewButtonColumn while keeping your code clean, extensible, and maintainable.