I want to set a value for a cell that is actually a comboboxcell. I done it for my another project but now the same way, doesnt work!!
here is my code
//dgExcel is my datagridview
var cmb = (DataGridViewComboBoxColumn)dgExcel.Columns[1];
cmb.DataSource = sucTurleri; //its a list
cmb.DisplayMember = "SucTuru";
cmb.ValueMember = "Id";
and this code is adding row to the grid
var Konumlar = ExcelYardimcisi.KonumlariExceldenAl(dg.FileName);
foreach (var konum in Konumlar)
{
dgExcel.Rows.Add(konum.KonumAdi, sucTurleri[0].SucTuru, DateTime.Now.ToShortDateString());
}
but i got an error and i used this
foreach (var konum in Konumlar)
{
dgExcel.Rows.Add(konum.KonumAdi, null, DateTime.Now.ToShortDateString());
DataGridViewComboBoxCell cbox = (DataGridViewComboBoxCell)dgExcel.Rows[dgExcel.Rows.Count - 1].Cells[1];
cbox.Value = sucTurleri[0].SucTuru;
}
and the error is
Your data binding part is pretty OK. The problem is with
dgExcel.Rows.Add()
method. Here is a sample code:Because the
DataGridViewComboBoxColumn
is data bound, you have to set the value byValueMember
, in my caseitems[1].Id
. You have to to this in your code as well.