C# .NET - How can i bind data to a datagridview combobox column?

Asked By Gayathri on 12-Jul-11 05:17 AM
 I have a datagridview one with combobox column and second with textbox column.Pls help me to bind data into datagridview combobox
Jitendra Faye replied to Gayathri on 12-Jul-11 05:22 AM

Try this code-

DataGridViewComboBoxCell dgvComBox = GetComboBox();

private void DataGridViewComboBoxCell GetComboBox()
{
 DataGridViewComboBoxCell  dgvComboBoxCell = new DataGridViewComboBoxCell();
dgvComboBoxCell .ToolTipText = "Some Text ";
dgvComboBoxCell.DataSource = "Your Datasource";
dgvComboBoxCell.DisplayMember = "DisplayColumnName";
dgvComboBoxCell ="ValueColumnName"
return dgvComboBoxCell ;
}

Thereafter you can assign this to datagridview cell in following manner:

dgvDataGridName.Rows[somecount].Cells[somecount]  = dgvComBox ;

Try and let me know.

TSN ... replied to Gayathri on 12-Jul-11 05:22 AM
hi...

write the following code in the EditingControlShowing Event corresponding DataGrid to raise the events of DataGridViewComboBoxColumn.

ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// Remove an existing event-handler, if present, to avoid
// adding multiple handlers when the editing control is reused.
combo.SelectedIndexChanged -=
new EventHandler(ComboBox_SelectedIndexChanged);

// Add the event handler.
combo.SelectedIndexChanged +=
new EventHandler(ComboBox_SelectedIndexChanged);
}

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;

//this way write code to binddata in DataGridViewComboBoxColumn as
data binded in a normal ComboBox.
}

Ravi S replied to Gayathri on 12-Jul-11 05:22 AM
HI

/ Define comboCompany variable as an object of ComboBoxColumn class
// and assign it to the DataGrid1 'Company' column.
ComboBoxColumn comboCompany = (ComboBoxColumn)DataGrid1.Columns[5];

XML Converter is available!
http://www.rustemsoft.com/xmla.htm
http://www.rustemsoft.com/download1.htm
http://www.rustemsoft.com/asp_datagrid_columns.asp#

// Define Name of Interface Page that will be called by clicking on ComboBox button
comboCompany.InterfacePageName = "ComboInterfacePage.aspx";
// Define width of Interface Page
comboCompany.InterfacePageWidth = 345;
// Define height of Interface Page
comboCompany.InterfacePageHeight = 400;
// Assign DataSource property for comboCompany object as tblCompanies
// data table where Companys’s names are stored.
comboCompany.DataSource = tblCompanies;
// Specify DisplayMember as "Name" field of tblCompanies table
comboCompany.DisplayMember = "Name";
// Specify ValueMember as "PubID" field of tblCompanies table
comboCompany.ValueMember = "PubID";
// Identify "Company" column's foreground color
comboCompany.ForeColor = Color.DarkMagenta;
// Identify "Company" column characters' font and size
comboCompany.Font_Name = "Tahoma";
comboCompany.Font_Size = FontUnit.Point(8);
// Adjust "Company" column's width
comboCompany.Width = Unit.Point(60);

refer the link for detailed explanation
http://www.rustemsoft.com/asp_datagrid_columns.asp
http://stackoverflow.com/questions/1390462/datagridviewcomboboxcolumn-name-value-how
Reena Jain replied to Gayathri on 12-Jul-11 05:45 AM
hi,

You could create a BindingList<> object to hold your objects and then bind that list to the three controls. Then setting the BindingContexts to different objects would let the 3 controls have different positions with a single list.

  
public partial class Form1 : Form
  {
  public Form1()
  {
    InitializeComponent();
  }
 
  private void Form1_Load(object sender, EventArgs e)
  {
    BindingList<MyObject> list = new BindingList<MyObject>();
    list.Add(new MyObject("Ramya", 43));
    list.Add(new MyObject("Manju", 43));
    list.Add(new MyObject("Gulnus", 43));
    list.Add(new MyObject("Sona", 43));
 
    dataGridView1.DataSource = list;
    comboBox1.DataSource = list;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "Name";
 
    comboBox2.DataSource = list;
    comboBox2.DisplayMember = "Name";
    comboBox2.ValueMember = "Name";
 
    comboBox1.BindingContext = new BindingContext();
    comboBox2.BindingContext = new BindingContext();
    dataGridView1.BindingContext = new BindingContext();
 
  }
  }
 
  public class MyObject
  {
  private string mName;
  private int mAge;
 
  public MyObject(string s, int i)
  {
    mName = s;
    mAge = i;
  }
  public string Name
  {
    get { return mName; }
    set { mName = value; }
  }
  public int Age
  {
    get { return mAge; }
    set { mAge = value; }
  }
  public override string ToString()
  {
    return Name;
  }
  }

hope this will help you
dipa ahuja replied to Gayathri on 12-Jul-11 07:14 AM
void bind()
{
  string q = "select ename from emp";
  SqlDataAdapter da = new SqlDataAdapter(q, "connectionString");
  DataTable dt = new DataTable();
  da.Fill(dt);
  //Craete combo column
  DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn();
  //Assing dataSource
  combo2.DataSource = dt;
  //Assing table Field on combo
  combo2.DisplayMember = "ename";
  combo2.ValueMember = "ename";
  combo2.HeaderText = "new combo";
  //Adding combo box column in dataGridView
  dataGridView1.Columns.Insert(4, combo2);     
}