Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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....

C#
private void TAB_Credit_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (TAB_Credit.Columns[e.ColumnIndex].Name == "Reference")
    {

         string cmdText = @"SELECT * FROM tb_ajout_articles 
                            WHERE Reference=@ref";
         MySqlDataAdapter sa = new MySqlDataAdapter(cmdText, MyConnexion);
         sa.SelectCommand.Parameters.Add("@ref", MySqlDbType.VarChar).Value =
         TAB_Credit.Rows[e.RowIndex].Cells["Reference"].Value.ToString();

         DataTable dt2 = new DataTable();
         sa.Fill(dt);

         if (dt.Rows.Count == 1)
         {
             TXT_Stock.Text = dt.Rows[0]["Quantite"].ToString();
             //double value = (double)TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value * (double)TAB_Credit.Rows[e.RowIndex].Cells["Prix_Unitaire"].Value;

             TAB_Credit.Rows[e.RowIndex].Cells["Designation"].Value = dt.Rows[0]["Designation"].ToString();
             TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value = dt.Rows[0]["Quantite"].ToString();
             TAB_Credit.Rows[e.RowIndex].Cells["Prix_Unitaire"].Value = dt.Rows[0]["Prix_Unitaire"].ToString();
             TAB_Credit["Total", e.RowIndex].Value = 
               Convert.ToString(
               Convert.ToInt32(TAB_Credit["Quantite", e.RowIndex].Value) * 
               Convert.ToInt32(TAB_Credit["Prix_Unitaire", e.RowIndex].Value));
               //TAB_Credit.Rows[e.RowIndex].Cells["Total"].Value = value.ToString();

             TXT_Brut.Text = TAB_Credit["Total", e.RowIndex].Value.ToString();

         }
     }
     if (TAB_Credit.CurrentCell.ColumnIndex == 6)
     {
         if (Convert.ToInt32(TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value) > Convert.ToInt32(TXT_Stock.Text))
         {
             MessageBox.Show("Stock finished");
             TAB_Credit.Rows[e.RowIndex].Cells["Quantite"].Value = dt.Rows[0]["Quantite"].ToString();

         }
         else
         {
             TAB_Credit["Total", e.RowIndex].Value = 
                   Convert.ToString(
                   Convert.ToInt32(TAB_Credit["Quantite", e.RowIndex].Value) * 
                   Convert.ToInt32(TAB_Credit["Prix_Unitaire", e.RowIndex].Value));
             TXT_Brut.Text = TAB_Credit["Total", e.RowIndex].Value.ToString();
         }
     }
 }

My 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
AB |AB |2 |1000 |2000 |5000
BC |BC |3 |1000 |3000

This is what I am trying to do... To do it, I tried this code:

C#
private void TAB_Credit_RowValidated(object sender, DataGridViewCellEventArgs e)
{
    int sum = 0;

    for (int i = 0; i < TAB_Credit.Rows.Count; ++i)
    {

        sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells["Total"].Value);
    }

    TAB_Credit.Rows[e.RowIndex].Cells["Total_Fact"].Value = sum.ToString();
}


What I have tried:

I tried but I am having trouble....
Posted
Updated 30-Apr-17 3:32am

1 solution

You are only populating the DataGridView if exactly one row is returned from the database...
C#
if (dt.Rows.Count == 1)< lang=
so check that you don't have more then one row being returned - debugging will help you determine this.

You also only have 6 columns on your table but you are looking at the 7th with
C#
if (TAB_Credit.CurrentCell.ColumnIndex == 6)
Remember that the column collection is zero-based - 1st column is 0, 2nd column is 1 etc.

To find out what is going on you will have to debug - put a breakpoint on the line
C#
if (TAB_Credit.Columns[e.ColumnIndex].Name == "Reference")


You haven't said what is wrong with your calculation of the total, but I can tell you that you are ignoring the first row with the for loop
C#
for (int i = 0; i < TAB_Credit.Rows.Count; ++i)
You are pre-incrementing i rather than post-increment. I believe that line should be
C#
for (int i = 0; i < TAB_Credit.Rows.Count; i++)
A subtle, but important difference. Again, you can confirm all of this by debugging. I believe I have already given you a link to an article on how to do that.
 
Share this answer
 
Comments
TatsuSheva 30-Apr-17 10:03am    
yes you are right, I had to remove if (dt.Rows.Count == 1)....
For the second question, the sum is done but if I click on another row I am having this error : System.InvalidCastException and the error come to this sum += Convert.ToInt32(TAB_Credit.Rows[i].Cells["Total"].Value); and this is the comment : {"Unable to cast a DBNull object in other types."}
CHill60 30-Apr-17 10:37am    
To need to check for the Value in the cell being null before trying to convert it to an integer. I personally abhor the Convert.Toxxx functions and much prefer int.TryParse... examples of TryParse[^]
TatsuSheva 30-Apr-17 10:41am    
Is it possible that it only shows the total not at each line ! I mean if you see the example in my case the first line in the total Bill there is 2000 and the second line 5000 but I want to be as I described in the question
CHill60 30-Apr-17 10:47am    
I don't understand your question. But you can calculated the total whenever you want and put it wherever you want on the grid. The DataGridView_Validated event is probably the best place to put it, as it means the user has finished entering data and all of the data entered has been validated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900