Chan
Chan

Reputation: 4291

Combobox does not show up and need to click twice in C#

I want to show an combobox and show all its elements when users click the cell in the datatable.

The problem is when I click the cell, the combobox does not appear.

But after I click the second cell, the combobox in the first cell appears.

Then, I need to click the combobox twice to display all the elements.

What is the problem and how to solve it?

Here is my code.

private DataTable GetInfoTable()
{
    DataTable l_dtInfo = new DataTable();
    l_dtInfo .Columns.Add("Info", typeof(string));
    l_dtInfo .Rows.Add("11-20");
    l_dtInfo .Rows.Add("21-30");
    l_dtInfo .Rows.Add("31-40");
    return l_dtInfo ;
}  

private void editValue(object sender, DataGridViewCellValidatingEventArgs e)
{
    DataGridView grid = sender as DataGridView;

    if (e.ColumnIndex > -1)
    {
        DataGridViewComboBoxCell l_objGridDropbox = new DataGridViewComboBoxCell();
        grid[e.ColumnIndex, e.RowIndex] = l_objGridDropbox;
        l_objGridDropbox.DataSource = GetInfoTable();
        l_objGridDropbox.ValueMember = "Info";
        l_objGridDropbox.DisplayMember = "Info";

     }  
 }

Upvotes: 0

Views: 330

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

The easier life (i.e. how MS intended you do it):

  • Add a DataSet type of file to your project
  • Open it so you see the design surface
  • Add a datatable representing your main data, e.g. Persons dataTable with Name(string) column, AgeRangeId (int) column; right click the surface, choose add>>datatable. Right click the table, add>>column etc
  • Add another table called AgeRanges, add an AgeRangeId(int) column and a DisplayText(string) column
  • Switch to forms designer, open DataSources window (hope youre using net framework, not net core!) from View menu>>Other windows
  • Drag the Persons node onto the form, DGV appears bound to a new bindingsource (at the bottom) which is bound to Persons datatable in the dataset that has appeared (at the bottom)
  • Click the [>] in the top right of the grid view to pop open its quick actions, choose edit columns
  • Change the AgeRange column to a combobox type one. Set the DataSource to the AgeRanges table, ValueMember to AgeRangeId, DisplayMember to DisplayText. DataMember should already be bound to AgeRangeId (in the Person table)
  • In code, in the constructor, fill in the AgeRange table like what you have there
private PersonForm() //constructor
{
    InitializeComponent();
 
    //whatever you chose to name your dataset, will affect this variable name
    someDataSetName.AgeRanges.AddAgeRangesRow(1, "11-20");
    someDataSetName.AgeRanges.AddAgeRangesRow(2, "21-30");
    someDataSetName.AgeRanges.AddAgeRangesRow(3, "31-40");

    //let's add some sample data to persons too
    someDataSetName.Persons.AddPersonsRow("John", 1);
    someDataSetName.Persons.AddPersonsRow("Mark", 3);
    someDataSetName.Persons.AddPersonsRow("Luke", 2);

}  

Run the project, and it should Just Work (TM).

Upvotes: 1

Related Questions