假设我拥有的数据是'valuenumber1'
,'valuenumber2'
,'valuenumber3'
。
我有一个问题,当选择'valuenumber2'
或'valuenumber3'
时,将创建下一行,但是上一行(具有'valuenumber2'
或'valuenumber3'
)将更改为'valuenumber1'
,这将导致所有创建的行具有仅'valuenumber1'
的值。
例如(请原谅我的图片编辑):
我的代码示例:
注意:“ cto”是与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);
}
我试图使用EditingControlShowing事件处理程序,但它不起作用:
private void dgvList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmb = e.Control as ComboBox;
if (cmb != null)
cmb.SelectedIndex = -1;
}
由于datagridview中的ComboBox没有SelectedIndex,是否存在我不知道的其他解决方案,即通过使用其他事件处理程序?
最佳答案
我认为您不希望以这种方式加载组合项。而是将该列绑定到您从数据库检索到的列表。像这样:
private void loadProjectList()
{
Result rs = GetList();
DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Select Data";
cmb.Name = "cmb";
cmb.MaxDropDownItems = 4;
cmb.DataSource = rs.LCI;
cmb.DisplayMember = "objectName";
cmb.ValueMember = "objectID";
dgvList.Columns.Add(cmb);
}