开发者

DataGridview Skip The Value Of First Row Column

开发者 https://www.devze.com 2023-01-19 01:47 出处:网络
The dataGridview first row column name amount is return no value as there is value inserted. For our Peers\' Convenient I draw the table as it\'s help to answer it better.

The dataGridview first row column name amount is return no value as there is value inserted.

For our Peers' Convenient I draw the table as it's help to answer it better.

my table on dataGridview is as below:

   private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name",typeof(string));
        dt.Columns.Add("amount",typeof(decima开发者_StackOverflow社区l));
        dataGridView1.DataSource = dt;
        dataGridView1.Columns[1].DefaultCellStyle.Format = "f2";
    }

Now I prepared the table and my main code is below:

  private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        decimal sum = 0.00m;

        for (int i = 0; i < dataGridView1.Rows.Count-1; i++) 
        {
                sum = sum + Convert.ToDecimal(dataGridView1[1, i].Value);
                textBox1.Text = sum.ToString("f2");
        }
    }

The above code throw Error like "Object Cannot cast from DBNull to other types". Even though there is value.

Now Look at to another Code:

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        decimal sum = 0.00m;

        for (int i = 0; i < dataGridView1.Rows.Count-1; i++) 
        {
            if (dataGridView1[1, i].Value != DBNull.Value) 
            {
                sum = sum + Convert.ToDecimal(dataGridView1[1, i].Value);
                textBox1.Text = sum.ToString("f2");

            }        

        }
    }

Now this code not throwing any Error but still the problem with it. The problem is when i enter the value on column of amount is look like it skip the value of first row's column name amount.

e.g....

I enter the value on column name amount is below:

1st Row column amount values is = 12
2nd Row column amount values is = 12
3rd Row column amount values is = 12

Now the total showing in textbox1 is = 24, where as actual total is = 36.

There is going something wrong with coding it shows that dataGridview's first row's column value is not accepted.


private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        decimal sum = 0.00m;

        for (int i = 0; i < dataGridView1.Rows.Count; i++) 
        {
            if (dataGridView1[1, i].Value != DBNull.Value) 
            {
                sum = sum + Convert.ToDecimal(dataGridView1[1, i].Value);
                textBox1.Text = sum.ToString("f2");

            }        

        }
    }

the change is in the condition in the for loop.When your loop condition is i < dataGridView1.Rows.Count - 1. You loop all the rows except the last one this is why you get 24 instead 36
Best Regards,
Iordan


Best Solution is as below:

  private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
    {
        decimal sum = 0.00m;

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            if (dataGridView1[1, i].Value != DBNull.Value)
            {
                sum = sum + Convert.ToDecimal(dataGridView1[1, i].Value);
                textBox1.Text = sum.ToString("f2");

            }

        }
    }

Just Change The dataGridView Event as above.

0

精彩评论

暂无评论...
验证码 换一张
取 消