Click here to Skip to main content
11,496,146 members (1,366 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C# .NET4.5
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 Smile | :)
Posted 14-Dec-12 14:03pm
jibesh at 14-Dec-12 20:27pm
are you inserting the record from database or just on the UI alone?
Pimpex Lo at 16-Dec-12 9:41am
no, I need jut into UI .
ahmad zrein at 15-Dec-12 1:05am
please clarify your question
Krunal Rohit at 15-Dec-12 1:56am
Elaborate your question...
-Krunal R.
Pimpex Lo at 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;
Pimpex Lo at 19-Dec-12 4:15am
thanks for trying but It's not what I'm looking for, thanks again :)
Akbar Ali Hussain at 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 :
        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)
                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;
                        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;
                if (SAME == false)

            //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]

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

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 208
3 Sascha Lefèvre 195
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100