1
Reply

What is the problem in my code?

Shovan Saha

Shovan Saha

Sep 12 2017 2:48 PM
169

C#, desktop application

I have a datagridview like this:

ID

Date

SoinikNo

APenName

Pension

Utsob

9

01/01/2017

L

A

3

2

16

05/02/2017

M

B

4

3

29

12/03/2017

L

A

5

7

33

17/04/2017

M

B

6

8

47

25/05/2017

L

A

3

5

52

13/06/2017

S

C

2

9

82

02/07/2017

S

C

1

12

I need a new datagridview like this:

Sl. No.

SoinikNo

APenName

Pension

Utsob

1

L

A

11

14

2

M

B

10

11

3

S

C

3

21

private void button1_Click(object sender, EventArgs e)
{
dataGridView2.Visible = true;
List _product = new List();
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 (1)