Some Guy
Some Guy

Reputation: 75

DataGridView binded to DataTable with ComboBox

I have DataGridView dgvData, which has two columns.

1 Column is type of DataGridViewComboBoxCell, I linked this column to DataSource of people.

People has Name and ID Properties, thus, I made the 1st column ValueMember as "ID" and DisplayMember as "Name".

Now, I want to link the DataTable to the DataGridView. The DataTable has 2 columns, PeopleName and PeopleCallPhone.

I want the bind will match the PeopleName to the 1st Column of my DataGridView and bind the CallPhone to the 2nd Column in my DataGridView.

After this, I want when I'm looping on my whole DataGridView to find only the value of my 1st Column, I mean the ID of the people (from the datasource of the Column 1 - People)

Can you help me guys?

Upvotes: 4

Views: 8896

Answers (1)

OhBeWise
OhBeWise

Reputation: 5454

Let's assume the following Database setup:

╔════════════════════════════╗    ╔═════════════════════════════════╗
║          People            ║    ║      Your DataTable Info        ║
╠════╦═══════════════════════╣    ╠═══════════════╦═════════════════╣
║ ID ║ Name                  ║    ║ PeopleName    ║ PeopleCallPhone ║
╠════╬═══════════════════════╣    ╠═══════════════╬═════════════════╣
║  1 ║ "John Smith"          ║    ║ "John Smith"  ║ 123-456-7890    ║
║  2 ║ "Jane Doe"            ║    ║ "Jane Doe"    ║ 234-567-8900    ║
║  3 ║ "Foo Bar"             ║    ║ "Foo Bar"     ║ 345-678-9000    ║
║  4 ║ "Justin Time"         ║    ║ "Justin Time" ║ 456-789-0000    ║
║  5 ║ "Imma Mann"           ║    ║ "Imma Mann"   ║ 567-890-0000    ║
╚════╩═══════════════════════╝    ╚═══════════════╩═════════════════╝

Also, let's assume your data structures to be:

List<People> people = GetPeopleFromDB();
DataTable table = GetDataTableInfoFromDB();

In order for the DataTable column "PeopleName" to coincide with the DataGridViewComboBoxColumn sourced from people, you must set DataGridViewComboBoxColumn.DataPropertyName. Because the values in that DataTable column match People.Name, that is the property you must set on DataGridViewComboBoxColumn.ValueMember. For example:

var col = new DataGridViewComboBoxColumn();
col.Name = "PeopleName";
col.DataPropertyName = "PeopleName";   // The DataTable column name.
col.HeaderText = "Name";
col.DataSource = people;
col.DisplayMember = "Name";
col.ValueMember = "Name";              // People.Property matching the DT column.
this.dataGridView1.Columns.Add(col);

this.dataGridView1.DataSource = table;
this.dataGridView1.Columns[1].HeaderText = "Phone";

Results:

Working example

As for your second issue, to find the ID of every entry while looping through each row, you will first want to grab the source for the ComboBoxColumn. Then you can loop through each row and using the value from the first column, find the associated ID to that value in the source. For example:

List<People> ppl = ((DataGridViewComboBoxColumn)this.dataGridView1.Columns[0]).DataSource as List<People>;

foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
    if (row.Index != this.dataGridView1.NewRowIndex)
    {
        var cell = row.Cells[0] as DataGridViewComboBoxCell;
        People person = ppl.SingleOrDefault(p => p.Name == cell.Value.ToString());

        if (person != null)
        {
            Console.WriteLine("{0} {1}, {2}", person.ID, person.Name, row.Cells[1].Value);
        }
    }
}

Output:

Console output

Upvotes: 8

Related Questions