Home > DeveloperSection > Articles > ComboBox in DataGridView in C#

ComboBox in DataGridView in C#


C# C# 
Ratings:
0 Comment(s)
 34227  View(s)
Rate this:

ComboBox in DataGridView in C#

In this article I have explained, how to use ComboBox (DataGridViewComboBoxColumn) within DataGridView using windows application C#. In this article I have added ComboBox in DataGridView through Code and Selected ComboBox value to insert SQL database. Below I have given screen shot of demo. Let’s come for creating this demo.

ComboBox in DataGridView in C#

Step 1: Create one Windows Forms Application and add DataGridView on Windows Form (For example form1) and assign new name of Form and DataGridView according your convenience as below image.

ComboBox in DataGridView in C#

Step 2: Now press F7 Key or open double click on form for open .cs file of windows form.

Step 3: Add System and System.Data.SqlClient namespace for database connectivity.

Step 4: Write below code within Load method.

//Create object of DataGridViewComboBoxColumn for adding ComboBox control

            DataGridViewComboBoxColumn dgvCmbWeek = new DataGridViewComboBoxColumn();

            {

                // Hearder name of column

                dgvCmbWeek.HeaderText = "Week"; 

                // Add items into Combobox

                dgvCmbWeek.Items.Add("Mondey");

                dgvCmbWeek.Items.Add("Tuesday");

                dgvCmbWeek.Items.Add("Wednesday");

                dgvCmbWeek.Items.Add("Thirsday");

                dgvCmbWeek.Items.Add("Friday");

                dgvCmbWeek.Items.Add("Saturday");

                // Column Index position of Controls

                dgvCmbWeek.DisplayIndex = 0;

            }

 

            //Create object of DataGridViewComboBoxColumn for adding ComboBox control

            DataGridViewComboBoxColumn dgvCmbArticles = new DataGridViewComboBoxColumn();

            {

                // Hearder name of column

                dgvCmbArticles.HeaderText = "Articles";

                // Add items into Combobox

                dgvCmbArticles.Items.Add("1");

                dgvCmbArticles.Items.Add("2");

                dgvCmbArticles.Items.Add("3");

                dgvCmbArticles.Items.Add("4");

                dgvCmbArticles.Items.Add("5");

                dgvCmbArticles.Items.Add("6");

                dgvCmbArticles.Items.Add("7");

                dgvCmbArticles.Items.Add("8");

                dgvCmbArticles.Items.Add("9");

                dgvCmbArticles.Items.Add("10");

                // Column Index position of Controls

                dgvCmbArticles.DisplayIndex = 1;

            }

 

            //Create object of DataGridViewComboBoxColumn for adding ComboBox control

            DataGridViewComboBoxColumn dgvCmbBlogs = new DataGridViewComboBoxColumn();

            {

                // Hearder name of column

                dgvCmbBlogs.HeaderText = "Blogs";

                // Add items into Combobox

                dgvCmbBlogs.Items.Add("1");

                dgvCmbBlogs.Items.Add("2");

                dgvCmbBlogs.Items.Add("3");

                dgvCmbBlogs.Items.Add("4");

                dgvCmbBlogs.Items.Add("5");

                dgvCmbBlogs.Items.Add("6");

                dgvCmbBlogs.Items.Add("7");

                dgvCmbBlogs.Items.Add("8");

                dgvCmbBlogs.Items.Add("9");

                dgvCmbBlogs.Items.Add("10");

                // Column Index position of Controls

                dgvCmbBlogs.DisplayIndex = 2;

            }

 

            //Create object of DataGridViewComboBoxColumn for adding ComboBox control

            DataGridViewComboBoxColumn dgvCmbIQs = new DataGridViewComboBoxColumn();

            {

                // Hearder name of column

                dgvCmbIQs.HeaderText = "IQs";

                // Add items into Combobox

                dgvCmbIQs.Items.Add("1");

                dgvCmbIQs.Items.Add("2");

                dgvCmbIQs.Items.Add("3");

                dgvCmbIQs.Items.Add("4");

                dgvCmbIQs.Items.Add("5");

                dgvCmbIQs.Items.Add("6");

                dgvCmbIQs.Items.Add("7");

                dgvCmbIQs.Items.Add("8");

                dgvCmbIQs.Items.Add("9");

                dgvCmbIQs.Items.Add("10");

                // Column Index position of Controls

                dgvCmbIQs.DisplayIndex = 3;

            }

 

            //Create object of DataGridViewComboBoxColumn for adding ComboBox control

            DataGridViewComboBoxColumn dgvCmbForums = new DataGridViewComboBoxColumn();

            {

                // Hearder name of column

                dgvCmbForums.HeaderText = "Forums";

                // Add items into Combobox

                dgvCmbForums.Items.Add("1");

                dgvCmbForums.Items.Add("2");

                dgvCmbForums.Items.Add("3");

                dgvCmbForums.Items.Add("4");

                dgvCmbForums.Items.Add("5");

                dgvCmbForums.Items.Add("6");

                dgvCmbForums.Items.Add("7");

                dgvCmbForums.Items.Add("8");

                dgvCmbForums.Items.Add("9");

                dgvCmbForums.Items.Add("10");

                // Column Index position of Controls

                dgvCmbForums.DisplayIndex = 4;

            }

 

            //Create object of DataGridViewComboBoxColumn for adding Button control

            DataGridViewButtonColumn dgvBtn = new DataGridViewButtonColumn();

            {

                // Hearder name of column

                dgvBtn.HeaderText = "Save";

                // Text of Button

                dgvBtn.Text = "Save";

                // Set size of button

                dgvBtn.Width = 80;

                // Column Index position of Controls

                dgvBtn.DisplayIndex = 5; // Position of Controls

                dgvBtn.UseColumnTextForButtonValue = true;

                dgvBtn.FlatStyle = FlatStyle.Standard;

            }

 

            //Add all controls into DataGridView

            dgvDeveloperSection.Columns.Add(dgvCmbWeek);

            dgvDeveloperSection.Columns.Add(dgvCmbArticles);

            dgvDeveloperSection.Columns.Add(dgvCmbBlogs);

            dgvDeveloperSection.Columns.Add(dgvCmbIQs);

            dgvDeveloperSection.Columns.Add(dgvCmbForums);

            dgvDeveloperSection.Columns.Add(dgvBtn);

 

Step 5: Create a table in your database for saving data. Table structure is given below.

Use avi

 

CREATE TABLE [dbo].[WeekTask]

(

[ID] INT IDENTITY PRIMARY KEY,

[Date] DATE NOT NULL,

[Week] VARCHAR(20) NOT NULL,

[Articles] INT NOT NULL,

[Blogs] INT NOT NULL,

[IQs] INT NOT NULL,

[Forums] INT NOT NULL

)

 

Step 6: Create CellContentClick event of DataGridView and write below code within this event.

// check event of button column

            if (e.ColumnIndex == 5)

            {

                // check condition of cambobox (either option are selected or not)

                if (dgvDeveloperSection.Rows[e.RowIndex].Cells[0].Value != null && dgvDeveloperSection.Rows[e.RowIndex].Cells[1].Value != null && dgvDeveloperSection.Rows[e.RowIndex].Cells[2].Value != null && dgvDeveloperSection.Rows[e.RowIndex].Cells[3].Value != null && dgvDeveloperSection.Rows[e.RowIndex].Cells[4].Value != null)

                {

                    // get current saved time

                    DateTime dDatetime = DateTime.Now.Date;

                    // get value of cells wtich row's button are clicked

                    string sWeek = dgvDeveloperSection.Rows[e.RowIndex].Cells[0].Value.ToString();

                    int nArticles = Convert.ToInt32(dgvDeveloperSection.Rows[e.RowIndex].Cells[1].Value.ToString());

                    int nBlogs = Convert.ToInt32(dgvDeveloperSection.Rows[e.RowIndex].Cells[2].Value.ToString());

                    int nIQs = Convert.ToInt32(dgvDeveloperSection.Rows[e.RowIndex].Cells[3].Value.ToString());

                    int nForums = Convert.ToInt32(dgvDeveloperSection.Rows[e.RowIndex].Cells[4].Value.ToString());

 

                    // connection string of database

                    SqlConnection con = new SqlConnection("Data Source = your database server name ; Initial Catalog = your database name; User Id = server id; Password = server password;");

                    // open connection

                    con.Open();

                    // sql command for save data into table

                    SqlCommand cmd = new SqlCommand("Insert into [dbo].[WeekTask]([Date],[Week],[Articles],[Blogs],[IQs],[Forums]) values('" + dDatetime + "','" + sWeek + "','" + nArticles + "','" + nBlogs + "','" + nIQs + "','" + nForums + "')", con);

                    // check inserted successfully

                    if (cmd.ExecuteNonQuery() > 0)

                    {

                        MessageBox.Show("Record Successfully!");

                    }

                    else

                        MessageBox.Show("Record not save!");

                    // close connection

                    con.Close();

                }

                else

                    MessageBox.Show("Select Option!");

            }

 

Step 7: Build your application and execute. Select option from all ComboBox then click on Save button. Now check your database, record are inserted.

 


Don't want to miss updates? Please click the below button!

Follow MindStick