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?

  • Maybe Project class to add a property of type Owner? class Project { .... Owner Owner } - Stepan Kasyanenko
  • @StepanKasyanenko, you can try. However, I still can't get the object from the DataGridViewComboBoxCell, and I only get the value. - iluxa1810
  • Then show by changing the question what you did and how. - Stepan Kasyanenko
  • @StepanKasyanenko, well, with this cast I only get the value, not the object var value = ((DataGridViewComboBoxCell) dataGridView1.Rows [e.RowIndex] .Cells [nameof (OwnerName)]). Value; - iluxa1810 September
  • one
    Please update your question and indicate in it what and how you changed. Do not write in the comments. - Stepan Kasyanenko

0