Reputation: 4291
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
Reputation: 74605
The easier life (i.e. how MS intended you do it):
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