How to add a Button each row in a DataGridView in C#

By FoxLearn 7/16/2024 9:06:35 AM   38.59K
To add a button to each row in a DataGridView in a C# Windows Forms application, you can follow these steps.

How to add a Button each row in a DataGridView in C#

To add a button column to a DataGridView in C#, you can follow these steps

Creating a new Windows Forms Application project, then open your form designer.

Drag and drop the DataGridView control from the Visual Studio toolbox drag the DataGridView control into your form designer, you can set the DockStyle property of the DataGridView to Fill.

c# datagridview

c# add button to datagridview cell

You can also add columns manually to the DataGridView from c# code

// Add columns to the DataGridView
dataGridView1.Columns.Add("Id", "Id");
dataGridView1.Columns.Add("Name", "Name");

// add button in datagridview c#
// Add a button column
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.HeaderText = "Action";
buttonColumn.Text = "Delete";
buttonColumn.UseColumnTextForButtonValue = true; // Set to true to use Text property for the button
dataGridView1.Columns.Add(buttonColumn);

// Sample data
dataGridView1.Rows.Add(1, "John");
dataGridView1.Rows.Add(2, "Alice");
dataGridView1.Rows.Add(3, "Bob");

In this post, i will create a Customer class to populate data from database

public class Customers
{
    public string CustomerID { get; set; }
    public string CustomerName { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
}

Adding a Form_Load event handler that allows you to initialize data, then add data to BindingSource.

// button add datagridview c#
private void Form1_Load(object sender, EventArgs e)
{
    //Init data
    customersBindingSource.Add(new Customers() { CustomerID = "1", CustomerName = "Maria Anders", Email = "[email protected]", Address = "Obere Str. 57" });
    customersBindingSource.Add(new Customers() { CustomerID = "2", CustomerName = "Ana Trujillo", Email = "[email protected]", Address = "Avada. de la Cons" });
    customersBindingSource.Add(new Customers() { CustomerID = "3", CustomerName = "Thomas Hardy", Email = "[email protected]", Address = "120 Hanover Sq." });
    customersBindingSource.Add(new Customers() { CustomerID = "4", CustomerName = "Elizabeth Liconln", Email = "[email protected]", Address = "23 Tsawassen" });
}

How to add edit and delete button in datagridview in c#

c# gridview add row button

Finaly, you can add a CellContentClick event handler to DataGridView that allows you to check the column name you click.

// datagridview button c# add
private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // Check deleted rows
    if (dataGridView.Columns[e.ColumnIndex].Name == "Delete")
    {
        if (MessageBox.Show("Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            customersBindingSource.RemoveCurrent();
    }
}

You can also subscribe to the CellClick event of the DataGridView to handle button clicks.

//Form_Load event handler
private void Form1_Load(object sender, EventArgs e)
{
    // Subscribe to the CellClick event
    dataGridView1.CellClick += DataGridView1_CellClick;
}

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Check if the clicked cell is a button cell and its column index is the button column
    if (e.ColumnIndex == dataGridView1.Columns["Delete"].Index && e.RowIndex >= 0)
    {
        // You can perform actions based on the row clicked
        MessageBox.Show($"Button clicked in row {e.RowIndex}");
    }
}

Check if the clicked cell is in the button column, and if so, perform the desired action.

You can view the video below, to know how to add button to datagridview in c# windows forms application.

VIDEO TUTORIAL