Accessing DataGridview columns

I have a datagridview that i want to capture two of the columns and use in a calculation. When the datagridview is populated it has 4 columns. I want to
capture the contents of the 3rd and 4th columns.

When I try, I get errors. I seriously need help. Below is a copy of the code:

Dim R As Integer
Dim total As Decimal

For R = 0 To Details1.Tables(0).Rows.Count - 1
total = total + CDec(DataGrid.Item(R, 2)) * CDec(DataGrid.Item(R, 3))
Next
txtTotal.Text = total.ToString("$##.00")

Comments

  • : I have a datagridview that i want to capture two of the columns and
    : use in a calculation. When the datagridview is populated it has 4
    : columns. I want to
    : capture the contents of the 3rd and 4th columns.
    :
    : When I try, I get errors. I seriously need help. Below is a copy of
    : the code:
    :
    : Dim R As Integer
    : Dim total As Decimal
    :
    : For R = 0 To Details1.Tables(0).Rows.Count - 1
    : total = total + CDec(DataGrid.Item(R, 2)) *
    : CDec(DataGrid.Item(R, 3))
    : Next
    : txtTotal.Text = total.ToString("$##.00")

    What kind of errors do you get? Compile or Runtime? Which error? On which line do the errors occur?
  • : I have a datagridview that i want to capture two of the columns and
    : use in a calculation. When the datagridview is populated it has 4
    : columns. I want to
    : capture the contents of the 3rd and 4th columns.
    :
    : When I try, I get errors. I seriously need help. Below is a copy of
    : the code:
    :
    : Dim R As Integer
    : Dim total As Decimal
    :
    : For R = 0 To Details1.Tables(0).Rows.Count - 1
    : total = total + CDec(DataGrid.Item(R, 2)) *
    : CDec(DataGrid.Item(R, 3))
    : Next
    : txtTotal.Text = total.ToString("$##.00")

    One issue is that you're using a TableRow index to reference a DataGrid row. It's not apparent how you're binding the data, however these indices are not guaranteed to be the same at the same time.
  • : : I have a datagridview that i want to capture two of the columns and
    : : use in a calculation. When the datagridview is populated it has 4
    : : columns. I want to
    : : capture the contents of the 3rd and 4th columns.
    : :
    : : When I try, I get errors. I seriously need help. Below is a copy of
    : : the code:
    : :
    : : Dim R As Integer
    : : Dim total As Decimal
    : :
    : : For R = 0 To Details1.Tables(0).Rows.Count - 1
    : : total = total + CDec(DataGrid.Item(R, 2)) *
    : : CDec(DataGrid.Item(R, 3))
    : : Next
    : : txtTotal.Text = total.ToString("$##.00")
    :
    : What kind of errors do you get? Compile or Runtime? Which error? On
    : which line do the errors occur?

    *************************************
    I get compile error as it tells me the line:
    total = total + CDec(DataGrid.Item(R, 2)) * CDec(DataGrid.Item(R, 3))
    is not valid.

    The exact msg is "Value of type System Windows.Forms.DataGridViewCell cannot be converted to Decimal"
  • : : I have a datagridview that i want to capture two of the columns and
    : : use in a calculation. When the datagridview is populated it has 4
    : : columns. I want to
    : : capture the contents of the 3rd and 4th columns.
    : :
    : : When I try, I get errors. I seriously need help. Below is a copy of
    : : the code:
    : :
    : : Dim R As Integer
    : : Dim total As Decimal
    : :
    : : For R = 0 To Details1.Tables(0).Rows.Count - 1
    : : total = total + CDec(DataGrid.Item(R, 2)) *
    : : CDec(DataGrid.Item(R, 3))
    : : Next
    : : txtTotal.Text = total.ToString("$##.00")
    :
    : One issue is that you're using a TableRow index to reference a
    : DataGrid row. It's not apparent how you're binding the data,
    : however these indices are not guaranteed to be the same at the same
    : time.

    Somehow the error is about the conversion of the decimal. Is there any System class to import at the beginning of the program?
  • : : : I have a datagridview that i want to capture two of the columns and
    : : : use in a calculation. When the datagridview is populated it has 4
    : : : columns. I want to
    : : : capture the contents of the 3rd and 4th columns.
    : : :
    : : : When I try, I get errors. I seriously need help. Below is a copy of
    : : : the code:
    : : :
    : : : Dim R As Integer
    : : : Dim total As Decimal
    : : :
    : : : For R = 0 To Details1.Tables(0).Rows.Count - 1
    : : : total = total + CDec(DataGrid.Item(R, 2)) *
    : : : CDec(DataGrid.Item(R, 3))
    : : : Next
    : : : txtTotal.Text = total.ToString("$##.00")
    : :
    : : One issue is that you're using a TableRow index to reference a
    : : DataGrid row. It's not apparent how you're binding the data,
    : : however these indices are not guaranteed to be the same at the same
    : : time.
    :
    : Somehow the error is about the conversion of the decimal. Is there
    : any System class to import at the beginning of the program?
    :

    That makes sense. DataGridView.Item(int, int) returns a DataGridViewCell object: http://msdn.microsoft.com/en-us/library/ms158656(VS.80).aspx. You'll want to do the following to get the value of the cell: [code]DataGrid.Item(R, 2).Value[/code]
  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

  • Hi guys
    I have a datagridview (unbound) that i want to calculate percentages based in a total. I already done the total (in a label.text). So what i want for the second column is to find out the percentage for each quantity that varies every time i enter a new quantity value.


    qty % OF Content

    10 8.130
    20 16.260
    30 24.390
    50 40.65
    13 10.569

    Total: 123


    Please i honestly need help.
    THANKS

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories