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

时间:2018-06-07 07:04:04

标签: c# winforms datagridview datagridviewcombobox datagridviewcomboboxcolumn

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

我遇到问题,当选择'valuenumber2''valuenumber3'时,将创建下一行,但上一行(包含'valuenumber2''valuenumber3')将更改为'valuenumber1'这会导致所有已创建的行的值仅为'valuenumber1'

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

enter image description here

我的代码示例:

  

注意:' 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,是否还有其他我不知道的解决方案,即使用其他事件处理程序?

1 个答案:

答案 0 :(得分:1)

我不认为你想以这种方式加载你的组合项目。而是将该列绑定到您从DB检索的列表。像这样:

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