C# Controls Datagridview Add Combobox Column - c# - c# tutorial - c# net



What is Datagridview ?

  • The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms.
  • The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior.
  • The DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types.
 Gridview Combobox

Gridview Combobox

Add Combobox Column:

  • In general, GridViewComboBoxColumn displays a set of predefined candidate text values in a drop down list.
  • This column type is typically used to provide a lookup into some set of relatively static values.
 c-sharp add combobox column
  1. Go to tool box and click on the DataGridview option the form will be open.
  2. When we click on the Collections, the String Collection Editor window will pop up where we can type strings. column collection is representing a collection of DataColumn objects for a DataTable.
  3. Click on "Add" button another Add column windows form will be open.
  4. After click on text box, and then select the DataGrideViewComboBoxColumn.
  5. And then click on the "Add" button.
 c-sharp add combobox column dataview
  1. Go to tool box and click on the DataGridview option the form will be open.
  2. When we click on the Collections, the String Collection Editor window will pop up where we can type strings. column collection is representing a collection of DataColumn objects for a DataTable.
  3. Click on "Add" button another Add column windows form will be open.
  4. Here we are giving YesOrNo, this name is added in the Selected Columns.
  5. Go to Unbound Columns properties select Item collection pop up String Collection Editor window is open.
  6. Here Yes, No is items name will be displayed.
  7. After click on the ok button.
 c-sharp add combobox column
  1. Next adding name in the Name text box, click on type box and then select the DataGrideViewTextBoxColumn in type text. After add name in the Header text box.
  2. Click on the "Add" button.
  3. "id" name is adding in Selected Columns windows form.
 c-sharp add column fetchdata
  1. After click on the ok button YeaOrNo, id and name table is opened.
 c-sharp add column  edit
  1. Double click on id in selected column.
  2. DataPropertyName (name of the property or database column associated with the DataGridViewColumn.) Here we are giving name, this id is added in the Selected Columns. Put "name" in DataPropertyName and then click on "ok" button.

C# Sample Code - C# Examples

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

namespace WikiTechy_Csharp_Windows_Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString =
                @"Data Source=VENKAT\KAASHIV;Initial Catalog=wikitechy;
                        user id = venkat;password=venkat";

            string sql = "SELECT * FROM login_table";

            SqlConnection connection = new SqlConnection(connectionString);

            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);

            connection.Open();
                        dataadapter.Fill(ds);
            connection.Close();

            dataGridView1.DataSource = ds.Tables[0]; 
        }
 

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            { 
                    string data = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
                    MessageBox.Show(data.ToString()); 
            }
            catch (Exception ex)
            {

            }
        }  
    }
}
click below button to copy the code. By - c# tutorial - team

Code Explanation:

 c-sharp add combobox samplecode
  1. DataSet ds = new DataSet(); specifies to Create a new instance of the DataSet class. The DataSet Object represents a complete set of data, including related tables, constraints, and relationships among the tables.
  2. string connectionString = @"Data Source=VENKAT\KAASHIV;Initial Catalog=wikitechy; user id = venkat;password=venkat"; connection string specifies that includes the source database name here the name is VENKAT\KAASHIV, and other parameters needed to establish the initial catalog connection is wikitechy. The default value is user id and password is venkat.In this example string sql = "SELECT * FROM login_table"; specifies to select the login_table in SQL server.
  3. SqlConnection connection = new SqlConnection (connectionString); this statement is used to acquire the SqlConnection as a resource.SqlDataAdapter dataadapter = new SqlDataAdapter (sql, connection); specifies to initializes a new instance of the SqlDataAdapter class with a SelectCommand (sql) and a SqlConnection object(connection).
  4. connection.Open(); specifies to Open the SqlConnection.dataadapter.Fill(ds); specifies to fetches the data from User and fills in the DataSet ds.connection.Close(); it is used to close the Database Connection.
  5. dataGridView1.DataSource = ds.Tables[0]; is used to specify the data source of datagridview and the table which will be bind to. When we run the code a window will show with a datagridview that containing all the returning rows.
  6. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) click event handler for Cellclick specifies to select a row by click a cell on the datagridview.
  7. try specified a try block identifies a block of code for which particular exceptions is activated.
  8. string data = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value; MessageBox.Show(data.ToString()); specifies to gets a value indicating the column and row index of the cell that event occurs for dataGridView1.
  9. MessageBox.Show(data.ToString()) displays a message box with the specified string. Here is returns this string data.
  10. catch (Exception ex) represent an error that occurs during application error.
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

Sample C# examples - Output :

 c-sharp edit fetch data
  1. Here in this output the Fetch Data button will display the data values from the SQL and displays it by clicking it. Here in this output table displays the YesorNo "Yes, No" drop down list, id "1,2,3" and name "venkat, jln, arun".
  fetch data
  1. In this output table displays the YesorNo "Yes" drop down list, id "1,2,3" and name "venkat, jln, arun".
  2. In this output table displays the "Yes" in 1 row 1 column message box window is open.

Add combobox programmatically in datagridview in c#

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.HeaderText = "Select Data";
            cmb.Name = "cmb";
            cmb.MaxDropDownItems = 4;
            cmb.Items.Add("True");
            cmb.Items.Add("False");
            dataGridView1.Columns.Add(cmb); 
click below button to copy the code. By - c# tutorial - team

 c-sharp data fetch output
  1. Go to tool box and click on the DataGridview option the form will be open.
  2. Click on the Collections, the String Collection Editor window will pop up where we can type strings.
  3. And the Edit Columns window is opened.

C# Sample Code - C# Examples

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

namespace WikiTechy_Csharp_Windows_Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString =
                @"Data Source=VENKAT\KAASHIV;Initial Catalog=wikitechy;
                        user id = venkat;password=venkat";

            string sql = "SELECT * FROM login_table";

            SqlConnection connection = new SqlConnection(connectionString);

            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);

            connection.Open();
                        dataadapter.Fill(ds);
            connection.Close();

            dataGridView1.DataSource = ds.Tables[0];
            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
            cmb.HeaderText = "YesOrNo";
            cmb.Name = "cmb"; 
            cmb.Items.Add("Yes");
            cmb.Items.Add("No");
            dataGridView1.Columns.Add(cmb);
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            { 
                    string data = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value;
                    MessageBox.Show(data.ToString()); 
            }
            catch (Exception ex)
            {

            }
        }  
    }
}
click below button to copy the code. By - c# tutorial - team

Code Explanation:

 c-sharp combobox grid output
  1. DataSet ds = new DataSet(); specifies to Create a new instance of the DataSet class. The DataSet Object represents a complete set of data, including related tables, constraints, and relationships among the tables.
  2. string connectionString = @"Data Source=VENKAT\KAASHIV;Initial Catalog=wikitechy; user id = venkat;password=venkat"; connection string specifies that includes the source database name here the name is VENKAT\KAASHIV, and other parameters needed to establish the initial catalog connection is wikitechy. The default value is user id and password is venkat.In this example string sql = "SELECT * FROM login_table"; specifies to select the login_table in SQL server.
  3. SqlConnection connection = new SqlConnection (connectionString); this statement is used to acquire the SqlConnection as a resource.SqlDataAdapter dataadapter = new SqlDataAdapter (sql, connection); specifies to initializes a new instance of the SqlDataAdapter class with a SelectCommand (sql) and a SqlConnection object(connection).
  4. connection.Open(); specifies to Open the SqlConnection.dataadapter.Fill(ds); specifies to fetches the data from User and fills in the DataSet ds.connection.Close(); it is used to close the Database Connection.
  5. dataGridView1.DataSource = ds.Tables[0]; is used to specify the data source of datagridview and the table which will be bind to. When we run the code a window will show with a datagridview that containing all the returning rows.
  6. DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn(); specifies to a new instance of the DataGridViewComboBoxColumn.cmb.HeaderText = "YesOrNo"; used to store the "YesOrNot" in DataTable.Here cmb.Name = "cmb"; specifies to text series of Unicode name "cmb". cmb.Items.Add("Yes"); Specifies to add the items "Yes" into the ComboBox.cmb.Items.Add("No"); Specifies to add the items "No" into the ComboBox.
  7. dataGridView1.Columns.Add(cmb); specifies to add combo in the column of a DataGridView , the column type contains cells of type DataGridViewComboColumn.
  8. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) click event handler for Cellclick specifies to select a row by click a cell on the datagridview.
  9. try specified a try block identifies a block of code for which particular exceptions is activated.
  10. string data = (string)dataGridView1[e.ColumnIndex, e.RowIndex].Value; MessageBox.Show(data.ToString()); specifies to gets a value indicating the column and row index of the cell that event occurs for dataGridView1.
  11. MessageBox.Show(data.ToString()) displays a message box with the specified string. Here is returns this string data.
  12. catch (Exception ex) represent an error that occurs during application error.

Sample C# examples - Output :

 c-sharp combobox grid output
  1. In this output table displays the id "1,2,3", name "venkat, jln, arun", and YesorNo "Yes" drop down list.
  2. In this output table displays the "Yes" in 1 row 3 column message box window is open.


Related Searches to C# Controls Datagridview Add Combobox Column