Click here to Skip to main content
15,119,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 1 grid view. On gridview cell click, it will show relavant data in another gridview.

Now another gridview has DataGridViewComboBoxColumn

Its data is loading but is is showing when i click on it.

Bu default it is showing blank.

Like this.

https://i.imgur.com/E8CHSBc.png[^][^]


It is showing blank. DataGridViewComboBoxColumn data are fetched is successfully.

They are showing when i click on that DataGridViewComboBoxColumn

Like this

https://i.imgur.com/qG75C5r.png[^]
[^]


what i want that it should also show data in first image

What I have tried:

C#
var cmbContactsName = new DataGridViewComboBoxColumn();

var dt2 = new DataTable();

dt2 = StMethod.GetListDT<InvoiceAddress>("SELECT (FirstName +' '+ LastName) as Contact,EmailAddress ,ContactsID FROM contacts where (IsDelete=0 or IsDelete IS NULL) AND Companyid=" + grdCompany.Rows[grdCompany.CurrentRow.Index].Cells["CompanyID"].Value.ToString());

cmbContactsName.DataSource = dt2;

cmbContactsName.ValueMember = dt2.Columns["ContactsID"].ToString();

cmbContactsName.DisplayMember = dt2.Columns["Contact"].ToString();

cmbContactsName.DataPropertyName = dt2.Columns["ContactsID"].ToString();
                
cmbContactsName.HeaderText = "Contacts Name";

cmbContactsName.ValueType = typeof(String);

cmbContactsName.DefaultCellStyle.NullValue = "";
Posted
Updated 6-Oct-21 2:50am
v2

Try just
cmbContactsName.ValueMember = "ContactsID";

cmbContactsName.DisplayMember = "Contact";

cmbContactsName.DataPropertyName = "ContactsID";

P.S.: "FirstName" is not a member of query column list, you can't address it as a DataTable column. Use "Contact" instead.
   
Comments
Devendra Sarang 6-Oct-21 5:01am
   
set the following but not working for me


cmbContactsName.ValueMember = dt2.Columns["ContactsID"].ToString();

cmbContactsName.DisplayMember = dt2.Columns["Contact"].ToString();

cmbContactsName.DataPropertyName = dt2.Columns["ContactsID"].ToString();
CHill60 6-Oct-21 5:35am
   
"but not working for me" ... That's because you are not following the instructions in the solution. Replace those three lines with the three lines from mirajanata's Solution
Devendra Sarang 6-Oct-21 6:14am
   
if i am using this

cmbContactsName.ValueMember = "ContactsID";

cmbContactsName.DisplayMember = "Contact";

cmbContactsName.DataPropertyName = "ContactsID";


then it is showing like this

https://i.imgur.com/okPneUy.png
Devendra Sarang 15-Oct-21 23:35pm
   
i have got answer.

Data is not showing because "cmbContactsName.ValueMember = dt2.Columns["ContactsID"].ToString()"
this one is throwing "Datagridviewcomboboxcell value is not valid" exception.

cmbContactsName.ValueMember = dt2.Columns["ContactsID"].ToString();
so i have commented that line and now data is showing in
but problem is that i have to use ValueMember value for further action
Try this order instead - it's the setting of DataSource that I've moved to after the ValueMember and DisplayMember
C#
cmbContactsName.ValueMember = "ContactsID";
cmbContactsName.DisplayMember = "Contact";
cmbContactsName.DataPropertyName = "ContactsID";
cmbContactsName.DataSource = dt2;
   
Comments
Devendra Sarang 6-Oct-21 7:14am
   
nope bro. same problem.
This is binding problem, maybe this helps:
cmbContactsName.DataSource = dt2.DefaultView;

instead of
cmbContactsName.DataSource = dt2;
   
Comments
CHill60 6-Oct-21 8:00am
   
Just a comment - it's usually best to use the "Improve solution" link on your first reply to add information / correct information. Otherwise it can be confusing for incoming readers
mirajanata 6-Oct-21 10:00am
   
OK, understood.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900