【问题标题】:count same selected values in datagridview and show count result in QTY column-C#在 datagridview 中计算相同的选定值并在 QTY 列中显示计数结果-C#
【发布时间】:2017-04-04 14:17:48
【问题描述】:

我有两个datagridview。

1)数据网格视图菜单

2)Dategrideviewfactor

每当我在菜单中选择一行时,该行将显示在dategridviewFactor..

我想要我在照片中显示的结果,我不想添加一个 qty=1 的新行!我想对选择行求和

1 点击菜单行--> QTY=1

2 点击菜单行-->QTY=2

3 点击菜单行-->QTY=3

。 . .

10 点击菜单行-->QTY=10

enter image description here

enter image description here



private void dataGridViewMenu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex==0)
            {

                SqlDataAdapter sda = new SqlDataAdapter(@"select FoodName,FoodCategory,FoodPrice from ResturanFood
                                                        where FoodName='" + dataGridViewMenu.CurrentRow.Cells[0].Value.ToString() + "'", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                foreach (DataRow item in dt.Rows)
                {
                    int n = dataGridViewFaktor.Rows.Add();
                    int jam = 1;
                    dataGridViewFaktor.Rows[n].Cells[0].Value = item[0].ToString();
                    dataGridViewFaktor.Rows[n].Cells[1].Value = item[1].ToString();
                    dataGridViewFaktor.Rows[n].Cells[2].Value = item[2].ToString();



                    dataGridViewFaktor.Rows[n].Cells[3].Value = jam;

                    foreach (DataGridViewRow row in dataGridViewFaktor.Rows)
                    {
                        row.Cells[dataGridViewFaktor.Columns["Column6"].Index].Value = (Convert.ToDouble(row.Cells[dataGridViewFaktor.Columns["Column5"].Index].Value) * Convert.ToDouble(row.Cells[dataGridViewFaktor.Columns["Column4"].Index].Value));
                    }
                }
            }
            SumColumn6();
        }

【问题讨论】:

  • 那里有问题吗?
  • 您需要分享行点击事件事件处理程序代码,以便我们了解您需要解决的问题。
  • @KernelMode 坦克供您评论,代码已添加。

标签: c# winforms datagridview


【解决方案1】:
            foreach (DataRow item in dt.Rows)
            {
                int n = dataGridViewFaktor.Rows.Add();

在上面的foreach 中,无论如何您都在向dataGridViewFaktor 添加一个新行而不是之前检查该行是否已经存在。如果该行已存在,则只需更新其数量列值。这样您将获得预期的结果。

要检查该行是否已经存在,您可以比较FoodName 列,或者如果您每行持有它,最好比较Food IDs。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多