3
votes

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?

1
The behavior can not be reproduced in a fully unbound DataGridview. But if you have a datagridview with some bound columns and some unbound columns, unbound columns will lost their values. Consider posting a minimal reproducible example.Reza Aghaei
Copy the following code and paste it in Load event of a clean form, run the application and see the result: var dgv = new DataGridView(); dgv.Dock = DockStyle.Fill; var combo = new DataGridViewComboBoxColumn(); combo.Items.AddRange(new string[] { "1", "2", "3" }); combo.HeaderText = "Combo"; dgv.Columns.Add(combo); this.Controls.Add(dgv); dgv.BringToFront();Reza Aghaei
That's why I said a minimal reproducible example is required. How could we know in which context you are doing your tests? How could we know what's the result of BusinessLayer.GetList();? Also it's not clear how your DataGridView has been set up.Reza Aghaei
You should be able to reproduce the problem using a really simple code like what I shared.Reza Aghaei

1 Answers

1
votes

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