I am still a little new to C# but I am using Winforms and I have a DataGridView which is connected to a datasource and is being populated correctly.
I have also added a ComboBoxColumn at run time. This ComboBoxColumn is being connected to a datasource, the displaymember and valuemember are set, the headertext is set and then the column is added to the datagrid. The connection works just fine and when the user clicks the comboBox is populated as expected.
When a user completes a new row in the grid he will obviously select the item in the comboBox and then the whole row is updated to my database when done.
My question is: When this datagrid is opened again or redrawn it is populated due to the datasource property, but how do I get the comboBox cell to display the VALUE he selected originally instead of just a blank box. I know the code of getting the value from the DB and put in a value. The ideal would be if I could put that variable in the display of the comboBox. Keep in mind that the comboBox is still data bound so that the user could edit the value if he so wishes?
I know that in a normal comboBox control I should just set the .Text property. But the DataGridViewComboBox does not have the same property.
Here is the code for the actual data connection and adding of the comboBoxColumn:
public void AddComboBoxColumn(DataGridView datagridName, DataTable table, string headerText, int columnIndex)
{
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
GetDisplayAndValueMembers(table, headerText); //Calls method that gets the datasource, displaymember, valuemember depending on column
column.DataSource = tableRef; //sets datasource to table referenced by column
column.DisplayMember = displayMember; //sets displaymember
column.ValueMember = valueMember; //sets valuemember
column.HeaderText = headerText; //changes headertext to displayed text
if (newColumnIndex == 0)
datagridName.Columns.Add(column); //added to end of datagrid
else
{
datagridName.Columns.RemoveAt(columnIndex);
datagridName.Columns.Insert(newColumnIndex, column); //added in specific index if needed
}
}
This just shows the dataconnection of the drop down. Obviously there are many methods being used with a large amount of code. But this is not the problem as this works fine. I don't know how to go about the issue of actually showing a previously selected item as the text of the comboBox?