Search code examples
c#winformsdatagridviewdatagridviewcomboboxdatagridviewcomboboxcolumn

Selected items in DataGridViewComboBoxColumn always retrieve the first value


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):

enter image description here

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?


Solution

  • I don't think you want to load your combo items that way. Rather, bind that column to the list you retrieve form you DB. Something like this:

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