Add ComboBox in DataGridView using C#

This C# program show how to add a ComboBox in cell of a DataGridView Control, Programmatically adding ComboBox on each row in the DataGridView.
First you need to create New Project and give the Name of project is GridViewCombobox. Add the command button and dataGridView control and copy this code and paste. So please change the datasource name,database name and Table name.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespaceGridViewCombobox
{
    public partial class Form1 : Form
    {
        stringcon = “Data Source=MachineName;Initial  Catalog=DataBaseName;Integrated Security=True”;
        publicForm1()
        {
            InitializeComponent();
        }
        privatevoid Form1_Load(objectsender, EventArgs e)
        {
            stringsql = “Select Employeeid,FirstName,LastName from Employees”;
            SqlConnectioncnn = new SqlConnection(con);
            SqlDataAdaptersd = new SqlDataAdapter(sql, cnn);
            DataSetds = new DataSet();
            cnn.Open();
            sd.Fill(ds, “Employees”);
            cnn.Close();
            dataGrid.DataSource = ds;
            dataGrid.DataMember = “Employees”;
        }
        privatevoid CmdAddbuttom_Click(object sender, EventArgs e)
        {
           
                    
            DataGridViewComboBoxColumnchkbtn = new  
            DataGridViewComboBoxColumn();
           
            chkbtn.Items.Add(“Male”);
            chkbtn.Items.Add(“Female”);
                       
            dataGrid.Columns.Add(chkbtn);
           
            chkbtn.HeaderText = “Gender”;
          
            chkbtn.Name = “Gender”;
           
           
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *