Hello,
I am having a lot of trouble with the datagridview. My datagridview holds data of the child table. It does populate fine. However the problem comes when I want to add a combobox to the datagrid. Toward the end of the code below I add a combobox and bind it's source to the appropriate look up table. When I run the application, the column is added and it does have the values of the look up table, however it does not show the currently selected value in the database. It comes up blank. Also, I use the following statement:
DgvCalls.AutoGenerateColumns =
false;My understanding is that this should not display any columns unless you add the columns programmatically. In addition to the column that I add, the columns from the datasource also come up.
Please Help!! Thanks a bunch
Yanci.
//parent table
DaCustomers = new SqlDataAdapter("select * from Customers", conn);
DaCustomers.Fill(data,
"Customers");//child table
DaCalls =
new SqlDataAdapter("select * from Calls", conn);DaCalls.Fill(data,
"Calls");//table with look up values to use in datagridview later
DaTblLkUp = new SqlDataAdapter("select * from tblLkUP", conn);
DaTblLkUp.Fill(data,
"tblLkUp");//relation
DataRelation relation = new DataRelation(" CustomersCalls",
data.Tables["Customers"].Columns["IDCall"],
data.Tables["Calls"].Columns["IDCall"],false);
data.Relations.Add(relation);
//BindSource1
BindSourceCustomers.DataSource = data;
BindSourceCustomers.DataMember =
"Customers";//BindSource2 relation in tables
BindSourceCalls.DataSource = BindSourceCustomers;
BindSourceCalls.DataMember =
"CustomersCalls";//DataGridView shows the related values of the parent table
DgvCalls.DataSource = BindSourceCalls;
DgvCalls.AutoGenerateColumns =
false; //Set up Column with the look up valueOne of the colums is called RsnCall. The main table stores 1, 2, 3 ,4....the second table stores the values to the codes "get info", "appointment", ect
DataGridViewComboBoxColumn Col1 = new DataGridViewComboBoxColumn();Col1.HeaderText =
"Reason Call";Col1.DataPropertyName =
"CustomersCalls.RsnCall";Col1.DataSource = data1;
Col1.DisplayMember =
"tblLkUp.Name_Lk";Col1.ValueMember =
"tblLkUp.ID_Lk";Col1.FlatStyle =
FlatStyle.Flat;DgvCalls.Columns.Insert(1, Col1);
What am I doing wrong

DataGridViewComboBoxColumn not displaying stored value
Sultan Shaikh
Thanks it worked!!
Do you know why the following is not working
DgvCalls.AutoGenerateColumns = false;
thanks a lot!!
GSK_phili
JBerggren
Col1.DataPropertyName = "RsnCall";
The Big Cat
Here is what MSDN says:
Columns are automatically generated when this property is set to true and the DataSource or DataMember properties are set or changed. Columns can also be automatically generated when the AutoGenerateColumns property is changed from false to true. If this property is true and the DataSource changes so there are columns that do not match the columns of the previous DataSource value, data in the unmatched columns is discarded. This property is ignored if the DataSource or DataMember properties are not set.
Don't forget to mark solution as answer.
Ravindra Agrawal - MSFT
I finally got it:
When I moved the statement:
DgvCalls.AutoGenerateColumns = false;
before
BindSourceCalls.DataSource = BindSourceCustomers;
then it runs okay. I think that if you bind first the false statement is ignored because you have already set the columns within your source.
What a relief!!