Jim Tat

Jim Tat

  • NA
  • 52
  • 45.8k

Filling datagridview cells after validating first row in C#

Apr 29 2017 4:26 PM
I have a DataGridView of 4-5 columns.
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.
But when I go to the next row and I want to add another value in the reference cell and then it will fill the other like the first one, it didn't do it...
This is what I tried....
 
  1. private void TAB_Credit_CellEndEdit(object sender, DataGridViewCellEventArgs e)  
  2. {  
  3.     if (TAB_Credit.Columns[e.ColumnIndex].Name == "Reference")  
  4.     {  
  5.    
  6.          string cmdText = @"SELECT * FROM tb_ajout_articles   
  7.                             WHERE Reference=@ref";  
  8.          MySqlDataAdapter sa = new MySqlDataAdapter(cmdText, MyConnexion);  
  9.          sa.SelectCommand.Parameters.Add("@ref", MySqlDbType.VarChar).Value =  
  10.          TAB_Credit.Rows[e.RowIndex].Cells["Reference"].Value.ToString();  
  11.    
  12.          DataTable dt2 = new DataTable();  
  13.          sa.Fill(dt);  
  14.    
  15.          if (dt.Rows.Count == 1)  
  16.          {  
  17.              TXT_Stock.Text = dt.Rows[0]["Quantite"].ToString();  
  18.              //double value = (double)TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value * (double)TAB_Credit.Rows[e.RowIndex].Cells["Prix_Unitaire"].Value;  
  19.    
  20.              TAB_Credit.Rows[e.RowIndex].Cells["Designation"].Value = dt.Rows[0]["Designation"].ToString();  
  21.              TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value = dt.Rows[0]["Quantite"].ToString();  
  22.              TAB_Credit.Rows[e.RowIndex].Cells["Prix_Unitaire"].Value = dt.Rows[0]["Prix_Unitaire"].ToString();  
  23.              TAB_Credit["Total", e.RowIndex].Value =   
  24.                Convert.ToString(  
  25.                Convert.ToInt32(TAB_Credit["Quantite", e.RowIndex].Value) *   
  26.                Convert.ToInt32(TAB_Credit["Prix_Unitaire", e.RowIndex].Value));  
  27.                //TAB_Credit.Rows[e.RowIndex].Cells["Total"].Value = value.ToString();  
  28.    
  29.              TXT_Brut.Text = TAB_Credit["Total", e.RowIndex].Value.ToString();  
  30.    
  31.          }  
  32.      }  
  33.      if (TAB_Credit.CurrentCell.ColumnIndex == 6)  
  34.      {  
  35.          if (Convert.ToInt32(TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value) > Convert.ToInt32(TXT_Stock.Text))  
  36.          {  
  37.              MessageBox.Show("Stock finished");  
  38.              TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value = dt.Rows[0]["Quantite"].ToString();  
  39.    
  40.          }  
  41.          else  
  42.          {  
  43.              TAB_Credit["Total", e.RowIndex].Value =   
  44.                    Convert.ToString(  
  45.                    Convert.ToInt32(TAB_Credit["Quantite", e.RowIndex].Value) *   
  46.                    Convert.ToInt32(TAB_Credit["Prix_Unitaire", e.RowIndex].Value));  
  47.              TXT_Brut.Text = TAB_Credit["Total", e.RowIndex].Value.ToString();  
  48.          }  
  49.      }  
  50.  }  
 y app is a sort of cash register for a shop so in the datagrid there is Reference ,
it is the reference of the product.
When the user types the reference directly the information about the product will be filled in the other columns and the user can choose the quantity he wants and it will do the calculation.

ALSO, I tried to put the total of the bill in the datagrid...

Reference | Product |Quantity |Price |Total |Total Bill
A               |AB          |2            |1000 |2000 |5000
BC            |B            |               |1000 |3000

This is what I am trying to do... To do it, I tried this code:
  1. private void TAB_Credit_RowValidated(object sender, DataGridViewCellEventArgs e)  
  2. {  
  3.     int sum = 0;  
  4.    
  5.     for (int i = 0; i < TAB_Credit.Rows.Count; ++i)  
  6.     {  
  7.    
  8.         sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells["Total"].Value);  
  9.     }  
  10.    
  11.     TAB_Credit.Rows[e.RowIndex].Cells["Total_Fact"].Value = sum.ToString();  
  12. }  
 
 

Answers (1)