Shovan Saha

Shovan Saha

  • NA
  • 321
  • 92.6k

Please update my code

Sep 12 2017 11:37 AM
C#, desktop application, visual studio 2015. 
I have a dataGridView like this:

Type

Item

Price

Quantity

L

A

3

2

M

B

4

3

L

A

5

7

M

B

6

8

L

A

3

5

S

C

2

9

S

C

1

12

 
I need another dataGridView like this:

Sl. No.

Type

Item

TotalPrice

TotalQuantity

1

L

A

11

14

2

M

B

10

11

3

S

C

3

21

 
I am trying like this but failed:
private void button1_Click(object sender, EventArgs e)
{
dataGridView2.Visible = true;
List<ProductList> _product = new List<ProductList>();
int Count = 1;
foreach (DataGridViewRow row in dataGridView1APen.Rows)
{
if (row.Cells[0].Value != "" && row.Cells[0].Value != null)
{
if (_product.Where(x => x.SoinikNo.ToLower() == row.Cells[0].Value.ToString().ToLower()).Any())
{
string Utsob = (Convert.ToInt32(_product.Where(x => x.SoinikNo.ToLower() == row.Cells[0].Value.ToString().ToLower()).Select(x => x.Utsob).FirstOrDefault()) + Convert.ToInt32(row.Cells[13].Value.ToString())).ToString();
string Pension = (Convert.ToInt32(_product.Where(x => x.SoinikNo.ToLower() == row.Cells[0].Value.ToString().ToLower()).Select(x => x.Pension).FirstOrDefault()) + Convert.ToInt32(row.Cells[12].Value.ToString())).ToString();
_product.Where(x => x.SoinikNo.ToLower() == row.Cells[0].Value.ToString().ToLower()).ToList().ForEach(x => { x.Utsob = Utsob; x.Pension = Pension; });
}
else
{
_product.Add(new ProductList
{
Sno = Count,
SoinikNo = row.Cells[2].Value.ToString(),
APenName = row.Cells[3].Value.ToString(),
Pension = row.Cells[12].Value.ToString(),
Utsob = row.Cells[13].Value.ToString()
});
Count++;
}
}
dataGridView2.DataSource = _product;
}
}

Answers (2)