Click here to Skip to main content
15,881,693 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 :)
Posted
Comments
Jibesh 14-Dec-12 20:27pm    
are you inserting the record from database or just on the UI alone?
Taulant Loshi 16-Dec-12 9:41am    
no, I need jut into UI .
ahmad zrein 15-Dec-12 1:05am    
please clarify your question
[no name] 15-Dec-12 1:56am    
Elaborate your question...
-Krunal R.
Taulant Loshi 16-Dec-12 9:46am    
ex. I have grid1 with items and grid2 that is null, so I want to add items from grid1 to grid2, but not duplicate the rows just increment quantity of the same item , I hope I'm more clear now :)

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;
 
Share this answer
 
Comments
Taulant Loshi 19-Dec-12 4:15am    
thanks for trying but It's not what I'm looking for, thanks again :)
Akbar Ali Hussain 19-Dec-12 12:06pm    
If you really want a solution, you have to explain what you want. Or whenever you get a solution, post here. That will help us to learn something new.
yes I have solved this myself, take a look here :
C#
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 = "";
}

[Edit]Code block added[/Edit]
 
Share this answer
 
v2

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