Jim Tat

Jim Tat

  • NA
  • 52
  • 47.2k

How to fill DataGridView Cells on cellvaluechanged in MYSQL

Apr 29 2017 5:31 AM
I have a DataGridView in which there are 4 columns; Reference,Quantity, Rate and Amount.
The DataGridView is Editable. When I enter a value in the Reference Column then immediately it will fill the other values in the others cells from mysql database.
This is what I tried....
  1. private void TAB_Credit_CellValueChanged(object sender, DataGridViewCellEventArgs e)  
  2.         {  
  3.   
  4.             try  
  5.             {  
  6.                 if (TAB_Credit.CurrentCell.ColumnIndex == 0)  
  7.                 {  
  8.   
  9.                     MySqlDataAdapter sa = new MySqlDataAdapter("SELECT * FROM table WHERE Reference='" + TAB_Credit.Rows[e.RowIndex].Cells["Reference"].Value + "'", MyConnexion);  
  10.                     DataTable dt2 = new DataTable();  
  11.   
  12.                     sa.Fill(dt2);  
  13.                       
  14.                         double value = (double)TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value * (double)TAB_Credit.Rows[e.RowIndex].Cells["PU"].Value;  
  15.   
  16.                         TAB_Credit.Rows[e.RowIndex].Cells["Designation"].Value = dt2.Rows[0]["Designation"].ToString();  
  17.                         TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value = dt2.Rows[0]["Quantite"].ToString();  
  18.                         TAB_Credit.Rows[e.RowIndex].Cells["PU"].Value = dt2.Rows[0]["Prix_Unitaire"].ToString();  
  19.                         TAB_Credit.Rows[e.RowIndex].Cells["Total"].Value = value.ToString();  
  20.   
  21.   
  22.                     }  
  23.                       
  24.             }  
  25.             catch  
  26.             { }  
  27.         }  
So in the datagrid, when I insert in the Reference Cell nothing is appearing in the other cells.
Thank you. 

Answers (2)