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
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.
- Go to tool box and click on the DataGridview option the form will be open.
- 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.
- Click on "Add" button another Add column windows form will be open.
- After click on text box, and then select the DataGrideViewComboBoxColumn.
- And then click on the "Add" button.
- Go to tool box and click on the DataGridview option the form will be open.
- 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.
- Click on "Add" button another Add column windows form will be open.
- Here we are giving YesOrNo, this name is added in the Selected Columns.
- Go to Unbound Columns properties select Item collection pop up String Collection Editor window is open.
- Here Yes, No is items name will be displayed.
- After click on the ok button.
- 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.
- Click on the "Add" button.
- "id" name is adding in Selected Columns windows form.
- After click on the ok button YeaOrNo, id and name table is opened.
- Double click on id in selected column.
- 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
Code Explanation:
- 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.
- 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.
- 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).
- 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.
- 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.
- 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.
- try specified a try block identifies a block of code for which particular exceptions is activated.
- 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.
- MessageBox.Show(data.ToString()) displays a message box with the specified string. Here is returns this string data.
- catch (Exception ex) represent an error that occurs during application error.
Sample C# examples - Output :
- 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".
- In this output table displays the YesorNo "Yes" drop down list, id "1,2,3" and name "venkat, jln, arun".
- In this output table displays the "Yes" in 1 row 1 column message box window is open.
Add combobox programmatically in datagridview in c#
- Go to tool box and click on the DataGridview option the form will be open.
- Click on the Collections, the String Collection Editor window will pop up where we can type strings.
- And the Edit Columns window is opened.
C# Sample Code - C# Examples
Read Also
Dot Net Training in chennai , .net course training in chennai , dot net course for beginnersCode Explanation:
- 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.
- 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.
- 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).
- 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.
- 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.
- 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.
- dataGridView1.Columns.Add(cmb); specifies to add combo in the column of a DataGridView , the column type contains cells of type DataGridViewComboColumn.
- 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.
- try specified a try block identifies a block of code for which particular exceptions is activated.
- 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.
- MessageBox.Show(data.ToString()) displays a message box with the specified string. Here is returns this string data.
- catch (Exception ex) represent an error that occurs during application error.
Sample C# examples - Output :
- In this output table displays the id "1,2,3", name "venkat, jln, arun", and YesorNo "Yes" drop down list.
- In this output table displays the "Yes" in 1 row 3 column message box window is open.