在C#中验证第一行后填充datagridview单元格 [英] Filling datagridview cells after validating first row in C#

查看:83
本文介绍了在C#中验证第一行后填充datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个4-5列的DataGridView。

DataGridView是可编辑的。

当我在参考列中输入一个值时,它会立即填充另一个来自mysql数据库的其他单元格中的值。

但是当我进入下一行并且我想在引用单元格中添加另一个值然后它将像第一行一样填充另一行时,它没有不要这样做......

这就是我试过的......



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....

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();
         }
     }
 }



我的应用程序是一种商店的收银机,因此在数据网格中有参考,

它是产品的参考。

当用户直接输入参考时,有关产品的信息将填入其他列,用户可以选择数量想要,它会进行计算。



此外,我试图把账单的总额放在数据网格中......



参考|产品|数量|价格|总计|总账单

AB | AB | 2 | 1000 | 2000 | 5000

BC | BC | 3 | 1000 | 3000



这就是我要做的......为了做到这一点,我尝试了这段代码:




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:

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....

推荐答案

如果从数据库中只返回一行,则只填充DataGridView。 ..
You are only populating the DataGridView if exactly one row is returned from the database...
if (dt.Rows.Count == 1)< lang=

因此请检查您没有返回多行 - 调试将帮助您确定这一点。



您的桌子上也只有6列,但是您正在查看第7行

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

if (TAB_Credit.CurrentCell.ColumnIndex == 6)

请记住,列集合从零开始 - 第1列为0,第2列是等等。



要了解发生了什么,你必须调试 - 在行上放一个断点

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

if (TAB_Credit.Columns[e.ColumnIndex].Name == "Reference")





您还没有说出计算总数有什么问题,但我可以告诉您,您忽略了第一行for循环



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

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

你是预先递增i而不是后递增。我认为该行应该

You are pre-incrementing i rather than post-increment. I believe that line should be

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.


这篇关于在C#中验证第一行后填充datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆