Assume the data I have are 'valuenumber1', 'valuenumber2', 'valuenumber3'.
I have a problem where when 'valuenumber2' or 'valuenumber3' is selected, the next row will be created but the previous row (having 'valuenumber2' or 'valuenumber3') will change to 'valuenumber1' and this leads to all created rows having value of only 'valuenumber1'.
For example (Please excuse my picture editing):
Example of my codes:
Note: 'cto' is Connection to Oracle
public class Result
{
public Result()
{
ds = new DataSet();
}
private DataSet ds;
public List<ComboItems> LCI { get; set; }
public DataSet ResultDataSet
{
get
{
return ds;
}
set
{
ds = value;
}
}
}
public class ComboItems
{
public string objectName { get; set; }
public string objectID { get; set; }
public ComboItems(){}
public ComboItems(string objectName, string objectID): this()
{
this.objectName = objectName;
this.objectID = objectID;
}
public override string ToString()
{
return objectName;
}
}
public Result GetList()
{
Result rs = new Result();
string commandText = "SELECT CUST_CODE, CUST_NAME FROM MAIN_CUSTOMER";
rs.ResultDataSet = cto.Select(commandText);
int maxRow = rs.ResultDataSet.Tables[0].Rows.Count;
List<ComboItems> lci = new List<ComboItems>();
for (int tblrow = 0; tblrow < maxRow; tblrow++)
{
ComboItems ci = new ComboItems();
ci.objectID = rs.ResultDataSet.Tables[0].Rows[tblrow]["CUST_CODE"].ToString();
ci.objectName = rs.ResultDataSet.Tables[0].Rows[tblrow]["CUST_NAME"].ToString();
lci.Add(ci);
}
rs.LCI = lci;
return rs;
}
private void loadProjectList()
{
Result rs = GetList();
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 4;
foreach (ComboItems ci in rs.LCI)
{
cmb.Items.Add(ci);
}
dgvList.Columns.Add(cmb);
this.dgvList.EditingControlShowing +=
new DataGridViewEditingControlShowingEventHandler(dgvList_EditingControlShowing);
}
I tried to use EditingControlShowing event handler but it does not work:
private void dgvList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmb = e.Control as ComboBox;
if (cmb != null)
cmb.SelectedIndex = -1;
}
As ComboBox in datagridview does not have SelectedIndex, is there other solutions which I am not aware of i.e by using other event handler?

DataGridview. But if you have a datagridview with some bound columns and some unbound columns, unbound columns will lost their values. Consider posting a minimal reproducible example. - Reza AghaeiLoadevent of a clean form, run the application and see the result:var dgv = new DataGridView(); dgv.Dock = DockStyle.Fill; var combo = new DataGridViewComboBoxColumn(); combo.Items.AddRange(new string[] { "1", "2", "3" }); combo.HeaderText = "Combo"; dgv.Columns.Add(combo); this.Controls.Add(dgv); dgv.BringToFront();- Reza AghaeiBusinessLayer.GetList();? Also it's not clear how yourDataGridViewhas been set up. - Reza Aghaei