6

Here are contacts but we can have multiple contacts so i want to show a list in combobox

DataTable dt = new DataTable();
dt = MainClass.GetDatabyQuery("select * from tbl");

if (dt.Rows.Count > 0)
{
    dgv_ClientDetail.DataSource = dt;
}

I have this method to fetch values from database in datagridview but I want one datagridview combobox column and want to bind data in one dgv combobox and other in dgv texboxes. If anybody know then tell me. Here are three columns Name, City , Contacts. I want to show multiple contacts in dgv combobox column

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vampire
  • 320
  • 1
  • 4
  • 10

3 Answers3

4

Just select only Name and City in dt, so that you can do something like,

        dgv_ClientDetail.DataSource = dt;
        DataGridViewComboBoxColumn dgvCboColumn = new DataGridViewComboBoxColumn();
        dgvCboColumn.Name = "Contacts";
        dgvCboColumn.DataSource = dtContacts; //DataTable that contains contact details
        dgvCboColumn.DisplayMember = "Name";
        dgvCboColumn.ValueMember = "Id";
        dataGridView1.Columns.Add(dgvCboColumn);

EDIT:

        dgv_ClientDetail.DataSource = new DataView(dt)
                                         .ToTable(true, new string[] { "Name", "City" });
        DataGridViewComboBoxColumn dgvCboColumn = new DataGridViewComboBoxColumn();
        dgvCboColumn.Name = "Contacts";
        dgv_ClientDetail.Columns.Add(dgvCboColumn);
        foreach (DataGridViewRow row in dgv_ClientDetail.Rows)
        {
            DataGridViewComboBoxCell cboContacts = (DataGridViewComboBoxCell)
                                                         (row.Cells["Contacts"]);
            cboContacts.DataSource = //Get the contact details of a person,
                                     //using his Name or Id field (row.Cells["Name"]);
            cboContacts.DisplayMember = "Name"; //Name column of contact datasource
            cboContacts.ValueMember = "Id";//Value column of contact datasource
        }

Hope this helps...

Vanest
  • 906
  • 5
  • 14
  • It is displaying but same contacts in each row. – Vampire Jun 12 '15 at 06:30
  • Yes...so are you about to set different contact set in each row? – Vanest Jun 12 '15 at 06:37
  • Yes In new row,there is new member with 2 contacts, I want to show these 2 contacts in his row in dgv combo. If any body has 1 contact and it should show 1 contact in combo – Vampire Jun 12 '15 at 06:46
  • Not Working. Giving Error "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewComboBoxCell' – Vampire Jun 12 '15 at 07:26
  • @user3355213 How about this? – Vanest Jun 12 '15 at 08:00
  • Check the datasources you are assigning in the `foreach` loop. That is, check the `cboContacts.DataSource` value each time when the loop iterates... – Vanest Jun 12 '15 at 09:04
  • Also could you please show how do you assign value to `cboContacts.DataSource`? – Vanest Jun 12 '15 at 09:12
  • Hai @Vanest . your solution really helped me a lot. But i have encountered a problem. When i click the header of a column to rearrange it in ascending order the combobox has no other value. My guess is when i click the header it will reset the combobox value. How can i solve this? – MRu Jun 27 '16 at 03:56
  • @Vampire it was because he forgot to tell you to set the `DataPropertyName` to the name of the column in the underlying data, too – Caius Jard Mar 23 '20 at 17:09
0

Try This:-

First just have data key in DataGridView for the contact. And on ItemDatabound Events just use the datakey for filter for each Dataitem with in that DataGridView.

Ashish Bisht
  • 292
  • 1
  • 10
-1

Try something like this

// Loop through rows and get each combobox

foreach (DataGridViewRow row in dgv_ClientDetail.Rows)
{
     DataGridViewComboBoxCell ContactCombo = (DataGridViewComboBoxCell)(row.Cells[index of Contact column]);

     ContactCombo.DataSource = // your contacts datasource;
     ContactCombo.DisplayMember = "name of field to be displayed like say ContactName";
     ContactCombo.ValueMember = "Id";
}

Edit: You need to set ValueMember for combobox as well

tariq
  • 2,193
  • 15
  • 26
  • It is giving error "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewComboBoxCell'. – Vampire Jun 12 '15 at 06:31
  • this means that textbox cell is getting picked, check that you are passing the correct index – tariq Jun 12 '15 at 06:37
  • indexes start from zero mind that. If the order of columns is Name, City and Contact . then Contact combobox column has index 2. – tariq Jun 12 '15 at 06:37