DataGridViewComboBoxColumn 中的选定项始终检索第一个值

Selected items in DataGridViewComboBoxColumn always retrieve the first value

假设我的数据是'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更改为 'valuenumber1',这会导致所有已创建的行的值仅为 'valuenumber1'

例如(请原谅我的图片编辑):

我的代码示例:

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);
}

我尝试使用 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);
}