C#4.5 Grdiview问题 [英] C# 4.5 Grdiview Problem

查看:77
本文介绍了C#4.5 Grdiview问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridveiw,我需要在其上插入一些项目,所以如果一个项目已经在datagridview中,如何增加它的数量和数量而不添加具有相同数据的另一行:)

I have a datagridveiw and I need to insert some items on it, so if an item is already in datagridview how to increase it''s count and it''s quantity without adding another row with the same data :)

推荐答案

添加行后,您可以根据需要为单元格指定值。以下代码将第一行和第一列的值增加1.您必须确保在添加之前特定单元格中有值。



After adding row you can assign values to the cell whichever you want. The following code will increment the value of first row and first column with 1. You have to make sure there is a value in the particular cell before adding it.

dataGridView1.Rows[0].Cells[0].Value = (int)dataGridView1.Rows[0].Cells[0].Value + 1;


是的我自己已经解决了这个问题,请看一下:

yes I have solved this myself, take a look here :
private void insertitem_toBill()
{
    DataGridViewRow row = new DataGridViewRow();
    // create cells and fill them from dgv1 data
    row.CreateCells(this.dgvBill, ID, NameTxtBox.Text, outPriceTxtBox.Text, PriceTax.Text, QuantityTxtBox.Text, TaxTxtBox.Text);

    if (dgvBill.Rows.Count == 0)
    {
        this.dgvBill.Rows.Add(row);
    }
    else
    {
        bool SAME = false;
        for (int i = 0; i < dgvBill.Rows.Count; ++i)
        {
            if (dgvBill.Rows[i].Cells[0].Value.ToString() == ID)
            {
                dgvBill.Rows[i].Cells[4].Value = (Convert.ToDouble(dgvBill.Rows[i].Cells[4].Value) + Convert.ToDouble(QuantityTxtBox.Text)).ToString();

                SAME = true;
            }
            try
            {
                decimal value1 = Convert.ToDecimal(dgvBill.Rows[i].Cells[3].Value);
                decimal value2 = Convert.ToDecimal(dgvBill.Rows[i].Cells[4].Value);
                dgvBill.Rows[i].Cells[6].Value = value1 * value2;
            }
            catch{}
        }
        if (SAME == false)
        {
            this.dgvBill.Rows.Add(row);
        }
    }


    //calculating sum of rows
    decimal sum = 0;
    for (int i = 0; i < dgvBill.Rows.Count; ++i)
    {
        sum += Convert.ToDecimal(dgvFatura.Rows[i].Cells[6].Value);
    }


    TotalTxtBox.Text = sum.ToString();
    SearchProductTxtBox.Text = "";
}



已添加代码块[/编辑]


Code block added[/Edit]


这篇关于C#4.5 Grdiview问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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