简体   繁体   中英

Add value in datagrid cell after value of combobox is selected c#

I am trying to create an application which outputs a simple text file. This is my first project in c#. I have created a data grid table. There are basically 5 cells in each row ,and the rows are added dynamically after the user input. User can change values in 2 of the 5 cells only. My problem lies here, In the third cell user has to select a value from combo box in cell 3, the values in cell 4 and 5 are to be populated after the value in combo box is selected. However I am unable to do that. Attached is the image for reference. 应用图片

You can try something like this:

//EditingControlShowing event handler for your dataGridView1
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e){
   ComboBox combo = e.Control as ComboBox;
   if(combo != null) combo.SelectedIndexChanged += GridComboSelectedIndexChanged;
}
private void GridComboSelectedIndexChanged(object sender, EventArgs e) {
   ComboBox combo = sender as ComboBox;
   //Your populating code here
   //you can access the selected index via combo.SelectedIndex
   //you can access the current row by dataGridView1.CurrentCell.OwningRow
   //then you can access to the cell 4 and cell 5 by dataGridView.CurrentCell.OwningRow.Cells[4] and dataGridView.CurrentCell.OwningRow.Cells[5]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM