Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
Comments
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 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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;
  Permalink  
Comments
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 = "";
        }
[Edit]Code block added[/Edit]
  Permalink  
v2

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

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2014
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