hi guys,

im a bit stuck here and wondered if anyone could help?

what i want to do is, after the DataGridView is populated (this part works fine) is to do a calculation on 2 columns
i.e

column 2       column 6
   1               2
   2               2

i need column 1 and 2 to add up thier values i.e

column 2        column 6
   3               4

then i want to multply column 1 and 2 so my label = 12

the code i have is

            int a = Convert.ToInt32(dGV.CurrentRow.Cells[2].Value);
            int b = Convert.ToInt32(dGV.CurrentRow.Cells[6].Value);
            int c = a * b;

            label4.Text = c.ToString();          

but this keeps saying there is no instance of the object :/

cheers in advance

Column 1 = Index 0
Column 2 = Index 1
Column 3 = Index 2
...
Column 6 = Index 5

So dgv.CurrentRow.Cells[1] would get you Column 2.

If you're actually looking up the right column and it still says that, then ensure that CurrentRow is actually set. To change the CurrentRow you must select a cell, CurrentRow will then be set to the row for that cell.

Otherwise, you can look up rows based on index. Note that you cannot set CurrentRow = Rows[1] as CurrentRow is read-only.

If this still isn't your problem, paste the exact exception text, including the inner exception message. This should tell you exactly what the problem is and where it is ;)

hiya, aahh yes, my bad about the columns and cells
i amended the cells to 1 & 5,
but still the same error, basically its saying there is no currentrow, and that i have to create an instance of it??

Ok, how are you trying to peform this operation? Is it by a row based on user input, or do you just want to calculate every row?

As I understand it well, you have a DataGridView with column headers and x rows. Now you want to show the total of every column in row x+1, am I right?

Oh yes I see the OP uses CurrentRow. If there is probably no cell selected, this will return null. Hence the error I guess.

Exactly that, so now we need to determine how the User chooses which row to calculate and we can provide the solution ;)

hi guys in mydatagridview i have vaious columns, i want to add up 2 columns,
qty column and price column, i want the qty times price, but in my DGV i have multiple rows, so in my understanding i was wanting to add up the values by column, then times them??? is that wrong??

 private void dgvCust()
        {   // This subroutine configures the look and behavior of the Data Grid
            dGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // Chooses the selection made to full row
            dGV.AutoGenerateColumns = true;          // Auto generates the columns
            dGV.AutoResizeColumns();                 // Auto generates the size of the columns
            dGV.RowHeadersVisible = true;            // Makes headers visable.
            dGV.Columns[3].HeaderText = "Ordered Date";    // 1st header name
            dGV.Columns[3].Width = 136;              // set column width to 136.
            dGV.Columns[3].ReadOnly = true;          // set 1st column to read only
            dGV.Columns[6].HeaderText = "Order Number";    // 1st header name
            dGV.Columns[6].Width = 136;              // set column width to 136.
            dGV.Columns[6].ReadOnly = true;          // set 1st column to read only
            dGV.Columns[5].HeaderText = "Customer ID";   // 2nd header name
            dGV.Columns[5].Width = 136;              // set column width to 136.
            dGV.Columns[5].ReadOnly = true;          // set 1st column to read only
            dGV.Columns[7].HeaderText = "Item ID"; // 3rd header name
            dGV.Columns[7].Width = 136;              // set column width to 136.
            dGV.Columns[7].ReadOnly = true;          // set 1st column to read only
            dGV.Columns[2].HeaderText = "Quantity";    // 4th header name
            dGV.Columns[2].Width = 136;              // set column width to 136.
            dGV.Columns[2].ReadOnly = true;          // set 1st column to read only
            dGV.Columns["SalesPersonID"].Visible = false;
            dGV.Columns["Name"].Visible = false;
            dGV.Columns["Price"].Visible = false;

            int a = Convert.ToInt32(dGV.CurrentRow.Cells[1].Value);
            int b = Convert.ToInt32(dGV.CurrentRow.Cells[5].Value);
            int c = a * b;

            label4.Text = c.ToString();          



        }

Do you want the total price or the price per item?

If you want to price per item, I think you should do it before you insert the data to your DGV. If you're pulling this information from a database, you can make the stored procedure do the work for you :)

Otherwise, you will need to perform a foreach to pull each row from the dataset to do your maths. Then you can update your label at the end.

Hi Ddanbe,
sorry for the delay,
that code snippet was exactlly what i was looking for :) thanks alot

:)

This question has already been answered. Start a new discussion instead.