3

假设我拥有的数据是'valuenumber1', 'valuenumber2', 'valuenumber3'.

我遇到一个问题,即何时何'valuenumber2''valuenumber3'选择下一行,但是上一个行(具有'valuenumber2'OR '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,是否还有其他我不知道的解决方案,即使用其他事件处理程序?

4

1 回答 1

1

我不认为你想以这种方式加载你的组合项目。相反,将该列绑定到您从数据库中检索的列表。像这样的东西:

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);
}
于 2018-06-07T19:11:19.583 回答