How to add a combobox to .Net GridView


Today we are going to discuss how to add a combobox control in .NET windows application gridview. Gridview is not a new concept in .NET Windows application and adding the combobox is little bit critical task here. Are you looking guideline how to add a combobox to .Net GridView

Learn How to add a combobox to .Net GridView


Below is the sample program to add the combobox control to Gridview and disply in it.
VB.NET

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("ID", "Product ID")
DataGridView1.Columns.Add("Name", "Product Name")
DataGridView1.Columns.Add("Description", "Description")
DataGridView1.Columns.Add("Price", "Price")
'---create a new bindingsource control---
Dim bindingsource As New BindingSource
'---add the items into the control---
bindingsource.Add("Type A")
bindingsource.Add("Type B")
bindingsource.Add("Type C")
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Types"
'---data bind it---
comboBoxCol.DataSource = bindingsource
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)

End Sub
End Class


C#:

dataGridView1.Columns.Add("ID", "Product ID");
dataGridView1.Columns.Add("Name", "Product Name");
dataGridView1.Columns.Add("Description", "Description");
dataGridView1.Columns.Add("Price", "Price");

BindingSource bindingsource=new BindingSource();
//---add the items into the control---
bindingsource.Add("Type A");
bindingsource.Add("Type B");
bindingsource.Add("Type C");
//'---create a combobox column---
DataGridViewComboBoxColumn comboBoxCol=new DataGridViewComboBoxColumn();

//---set the header---
comboBoxCol.HeaderText = "Types";
//---data bind it---
comboBoxCol.DataSource = bindingsource;
//---add a combobox column to the DataGridView control---
dataGridView1.Columns.Add(comboBoxCol);


Above code snippet demonstrate to add the columns to Gridview by using BindingSource and bind the DataGridViewComboBoxColumn to it.
Below is the screen shot.
Combobox in Gridview


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: