Windows Forms: How to add a Button each row in a DataGridView in C#

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

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "AddButtonToDataGridView" and then click OK

add button each row to datagridview in c#Step 2: Design your form as below

c# datagridview

Step 3: Create a Customer class to map data

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

Step 4: Add code to handle your form as below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AddButtonToDataGridView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Init data
            customersBindingSource.Add(new Customers() { CustomerID = "1", CustomerName = "Maria Anders", Email = "maria@gmail.com", Address = "Obere Str. 57" });
            customersBindingSource.Add(new Customers() { CustomerID = "2", CustomerName = "Ana Trujillo", Email = "ana171@gmail.com", Address = "Avada. de la Cons" });
            customersBindingSource.Add(new Customers() { CustomerID = "3", CustomerName = "Thomas Hardy", Email = "thomad@gmail.com", Address = "120 Hanover Sq." });
            customersBindingSource.Add(new Customers() { CustomerID = "4", CustomerName = "Elizabeth Liconln", Email = "elizabeth@gmail.com", Address = "23 Tsawassen" });
        }

        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();
            }
        }
    }
}

VIDEO TUTORIALS