0
votes

I am trying to add a drop down list into all cells for a column in a datagridView in my Winforms Application. I have created the view programatically by dragging the dgv into the form, and adding columns upon the creation of the class.

The drop down list appears in my DGV column, however when I click on the list nothing opens up - i am unsure if the ComboBox is populated and not opening, or if it simply does not contain values for user to select. Hovering over the object when adding the comboBox to the cell, i can see the item array populated with my values..

Please advise on how I can allow the user to select values in the cell that are populated from my list

            DataGridViewTextBoxColumn diffKey = new DataGridViewTextBoxColumn();
            DataGridViewComboBoxColumn matchDesc = new DataGridViewComboBoxColumn();
            DataGridViewTextBoxColumn permDiff = new DataGridViewTextBoxColumn();

        grdEditAnnot.Columns.Add(diffKey);
        grdEditAnnot.Columns.Add(matchDesc);
        grdEditAnnot.Columns.Add(permDiff);

        DataGridViewComboBoxCell matchDescDropbox = new DataGridViewComboBoxCell();

        List<string> lstMatchDesc = new List<string>();

        LoadMatchDescriptions(out lstMatchDesc);


        foreach (string ddlItem in lstMatchDesc)
        {
            matchDescDropbox.Items.Add(ddlItem);
        }

        grdEditAnnot.Rows.Add(2);


        matchDescDropbox.DataSource = GetMatchDescDDL(); // Bind combobox with datasource.  
        matchDescDropbox.ValueMember = "Description";
        matchDescDropbox.DisplayMember = "Description";

        grdEditAnnot.Rows[0].Cells[1] = matchDescDropbox;

    }

    private DataTable GetMatchDescDDL()
    {
        DataTable l_dtDescription = new DataTable();
        l_dtDescription.Columns.Add("Description", typeof(string));

        List<string> matchDescLst;

        if (LoadMatchDescriptions(out matchDescLst) == 0)
        {
            foreach(string matchDesc in matchDescLst)
            {
                l_dtDescription.Rows.Add(matchDesc);
            }
        }

        return l_dtDescription;
    }

    private int LoadMatchDescriptions(out List<string> matchDscLst)
    {
        //string SQL = SQL USED TO POPULATE MY LIST 
        //a temporary list of test strings would work to show how to get this to work... ex: list of test1, test2, test3 etc 


        try
        {
            matchDscLst = new List<string>();
            rdr.ExecuteSQL(SQL);
            while ((rec = rdr.FetchNextRecord()) != null)
            {
                matchDscLst.Add(rec.GetStringVal(0));
            }

            return 0;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error Loading Match Descriptions", MessageBoxButtons.OK, MessageBoxIcon.Error);

            matchDscLst = new List<string>();
            return -1;
        }
    }

This is my first post on StackOverflow; apologies if this is not in best format or if we need more info..

Result of code: Grid that is created; user cannot open up comboBox or type into textbox columns

1
A DataGridViewComboBoxColumn (your matchDesc. BTW, you use the same name for a sting later on in foreach(string matchDesc in matchDescLst)) already contains DataGridViewComboBoxCells (i.e., you don't need matchDescDropbox at all. BTW, you first add Items to this ComboBox, then set its DataSource. Pick one method). Just set the DataSource of your matchDesc Column (and don't use that name for anything else in the same context).Jimi
I found that my DataGridView was set to ReadOnly. This was not allowing user to open up ComboBox. Thank you all for the responses and explanations, was very helpful in understanding what is going on.HULL3399

1 Answers