Meena S

Meena S

  • NA
  • 48
  • 4.3k

autofetch based on combobox selection in datagridview

May 8 2019 12:03 PM
I have 4 columns in datagrid.
 
sno Itemcode description uom in which itemcode is datagridcombobox
 
I want to fill datagrid other columns based on itemcode columncombobox selected value.
Am getting error in the below code and give me some idea to acheive this logic.
  1. protected void bindvalues()  
  2. {  
  3. OleDbConnection con = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=DESKTOP\SQLEXPRESS;User ID=sa; Password = aaa; Initial Catalog=bbb; Integrated secutity=SSPI");  
  4. DataSet ds = new DataSet();  
  5. con.Open();  
  6. OleDbCommand cmd = new OleDbCommand(("Select itemcode from Item"), con);  
  7. OleDbDataAdapter da = new OleDbDataAdapter();  
  8. da.SelectCommand = cmd;  
  9. DataTable dt = new DataTable();  
  10. da.Fill(ds);  
  11. dataGridView1.DataSource = ds;  
  12. DataGridViewComboBoxColumn ic = new DataGridViewComboBoxColumn();  
  13. ic.HeaderText = "ItemCode";  
  14. ic.Name = "ItemCode";  
  15. ic.MaxDropDownItems = 6;  
  16. ic.DataSource = ds.Tables[0];  
  17. ic.DisplayMember = "ItemCode";  
  18. dataGridView1.Columns.Insert(1, ic);  
  19. this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);  
  20. con.Close();  
  21. }  
  22. private void editGridCellComboBox_SelectedIndexChanged(object sender, EventArgs e)  
  23. {  
  24. if (editGridCellComboBox.SelectedItem != null)  
  25. {  
  26. ComboBox cb = (ComboBox)sender;  
  27. int result = ((ComboBox)sender).SelectedIndex;  
  28. editGridCellComboBox.SelectedIndexChanged -= editGridCellComboBox_SelectedIndexChanged;  
  29. if (cb.SelectedItem == null)  
  30. return;  
  31. //if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name = "Itemcode")  
  32. //{  
  33. // string Description = ((DataRowView)editGridCellComboBox.SelectedItem).Row["Itemcode"].ToString(); **not able to get selected value  
  34. //}  
  35. //DataRow row = (dataGridView1.CurrentRow.DataBoundItem as DataRowView).Row;  
  36. //String val = (String)(cb.SelectedItem as DataRowView).Row("description"); ***this logic also not working  
  37. OleDbConnection con = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=DESKTOP\SQLEXPRESS;User ID=sa; Password = aaa; Initial Catalog=bbb; Integrated secutity=SSPI");  
  38. DataSet ds = new DataSet();  
  39. OleDbCommand cmd1 = new OleDbCommand(("Select t2.description from Item t1 INNER JOIN ItemGroup t2 ON t1.Itemcode = '" + cb.SelectedItem.ToString() + "'"), con);  
  40. OleDbDataAdapter da = new OleDbDataAdapter();  
  41. da.SelectCommand = cmd1;  
  42. da.Fill(ds);  
  43. dataGridView1.Columns[2].DataPropertyName = "Itemdescription";  
  44. editGridCellComboBox.SelectedIndexChanged += editGridCellComboBox_SelectedIndexChanged;  
  45. }  
  46. }  
  47. ComboBox editGridCellComboBox = new ComboBox();  
  48. private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)  
  49. {  
  50. editGridCellComboBox = (ComboBox)e.Control;  
  51. if ((editGridCellComboBox != null))  
  52. {  
  53. editGridCellComboBox.SelectedIndexChanged += editGridCellComboBox_SelectedIndexChanged;  
  54. }  
  55. }  

Answers (1)