Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting issue while tab reach to next new row after loading data from data base to datagridview. Can anyone correct my code??


What I have tried:

C#
private void dataGridSALEITEM_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        decimal TOTALAMOUNT = 0;
        decimal TOTALCGST = 0;
        decimal TOTALSGST = 0;
        decimal NETTOTAL = 0;

        foreach (DataGridViewRow row in dataGridSALEITEM.Rows)
        {

            row.Cells[dataGridSALEITEM.Columns[7].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[3].Index].Value) * Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[4].Index].Value));

            row.Cells[dataGridSALEITEM.Columns[8].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[7].Index].Value) / 200 * Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[5].Index].Value));

            row.Cells[dataGridSALEITEM.Columns[9].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[7].Index].Value) / 200 * Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[5].Index].Value));

            row.Cells[dataGridSALEITEM.Columns[10].Index].Value = (Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[7].Index].Value) + Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[8].Index].Value) + Convert.ToInt32(row.Cells[dataGridSALEITEM.Columns[9].Index].Value));

            TOTALAMOUNT += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[7].Index].Value);
            TOTALCGST += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[8].Index].Value);
            TOTALSGST += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[9].Index].Value);
            NETTOTAL += Convert.ToDecimal(row.Cells[dataGridSALEITEM.Columns[10].Index].Value);
        }
        txtSUBTOTAL.Text = TOTALAMOUNT.ToString();
        txtCGST.Text = TOTALCGST.ToString();
        txtSGST.Text = TOTALSGST.ToString();
        txtGTOTAL.Text = NETTOTAL.ToString();
    }
Posted
Updated 13-Apr-20 9:34am

You could try this:
foreach (DataGridViewRow row in dataGridSALEITEM.Rows)
{
  if (row != null)
  {
  ... your code
  }
}

If you still get errors, you should test the other values for null.
Also try stepping through your code after setting a breakpoint, e.g. on the line with the foreach statement.
You can inspect the values of variables by hovering over them with the mouse.
 
Share this answer
 
v2
It's going to depend on the data - which we have no access to - which part of that code is wrong, or your DB design needs improvement.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Somewhere in that code one or more of your DB columns contains a NULL value, which is returned as DBNull, and the system is complaining that you are trying to convert that to another type such as an integer - and integers can't contain NULL so it is absolutely right to do so.

Why is is NULL? Don't know - no access to your DB, remember - so the first thing to do is find out what value it is, then look at the whole row and work out if NULL is an appropriate value. If it is, then check for it and do what makes sense as a result. If it isn't, then work out how it got to be, what it should contain, and change your DB so it can't be NULL at all.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900