There are 2 classes:
one)
class Project { string ProjectName int OwnerId string OwnerName }
2)
Class Owner { string OwnerName int OwnerId }
There is a DataGridView, which consists of 2 fields:
ProjectName, which is a regular text field and OwnerName, which is a ComboBox.
On the DataGridView bindits List, and on the ComboBox in the DataGridView bindits List.
I want, when choosing from the ComboBox'a element, to update with these values the element in
List (Assign OwnerId and OwnerName).
Tell me, how can this be done?
I made a crutch like this and for every change, I have to use my hands to pull these values out of the DataSet, which is connected to the ComboBox.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.RowCount == 0) { return; } var editedItem = (Project)dataGridView1.Rows[e.RowIndex].DataBoundItem; var value = ((DataGridViewComboBoxCell) dataGridView1.Rows[e.RowIndex].Cells[nameof(OwnerName)]).Value; var result = ((List<Owner>) Owner.DataSource).Single(x => x.OwnerName == value); editedItem.OwnerId = result.OwnerId; editedItem.OwnerName = result.OwnerName; }
Tell me, is it possible to do something less crutally?
class Project { .... Owner Owner }
- Stepan Kasyanenko