Solved

# Datagridview Col1+Col2*Col3 = Textbox value

Posted on 2013-01-12
639 Views
Software vb.net & sql server 2005......databound datagridview.

I am looking for sample code for the following:
Datagridview Col1+Col2*Col3 = Textbox value

Thanks!
0
Question by:dave_sky
• 15
• 9
• 7

LVL 42

Expert Comment

ID: 38771325
>>Col1+Col2*Col3 = Textbox value
what is that mean?
0

Author Comment

ID: 38772140
Databound Datagridview:

Col1   | Col2   | Col3  |
2     |    3      |    2     |

(2 + 3) times 2 = Textbox1 value (10)
0

LVL 42

Expert Comment

ID: 38772175
Is it always the 1st row?
Basically it is something like this:
``````txtbox.Value=(gridview.Rows[0][0]+ gridview.Rows[0][1])* gridview.Rows[0][2]
``````

The 1st index after Rows is the row index.
So this code calculates the equation on 1st row data.
Replace square brackets with round brackets to have it vb.net
0

Author Comment

ID: 38772208
I should have been more clear - the calculations involve multiple rows...

Databound Datagridview:

Col1   | Col2   | Col3  |
2     |    3      |    2     |
2     |   3       |    2     |

Row 1 (2 + 3) times 2
Row 2 (2 + 3) times 2

Textbox1 value = (20)

Maybe, I have to use another 'extension' column...
0

LVL 42

Expert Comment

ID: 38772512
ok, so u want to sum all rows by the formula you provided.

the simple way:
``````Dim sum As Long = 0
For Each drg As DataGridRow In Datagridview.Rows.Cast(Of DataGridRow)()
sum += (drg(0) + drg(1)) * drg(2)
Next
Textbox1.Value = sum.ToString()
``````

the linq way:
``````Dim rowsSum As [Long] = Datagridview.Rows.Cast(Of DataGridRow)().[Select](Function(n) ([Long].Parse(n(0).ToString()) + [Long].Parse(n(1).ToString())) * [Long].Parse(n(2).ToString()))
Textbox1.Value = rowsSum.Sum().ToString()
``````
0

Author Comment

ID: 38772730
Is this how you had the sample code result planned?

Dim sum As Long = 0
For Each drg As DataGridViewRow In TblPlumbingBidDetailDataGridView.Rows.Cast(Of DataGridViewRow)()
sum += (drg(7) + drg(11)) * drg(3)
Next
TextBoxEstFiMaterials.Value = sum.ToString()
0

LVL 42

Expert Comment

ID: 38773475
yes.

make sure you got the column index correct, but its exactly right.

btw, i made a mistake in the linq example, here's the right one:

``````Textbox1.Value = Datagridview.Rows.Cast(Of DataGridRow)().[Select](Function(n) ([Long].Parse(n(0).ToString()) + [Long].Parse(n(1).ToString())) * [Long].Parse(n(2).ToString())).Sum().ToString()
``````
0

LVL 83

Expert Comment

ID: 38773998
Is the grid bound to a datatable? If yes then try

DataTable1.Columns.Add("Total", GetType(Double), "(Column1 + Column2) * Column3")

Dim Total = DataTable1.Compute("Sum(Total)"
0

Author Comment

ID: 38775131
Sedgwick, I couldn't get you code to work.

Codecruiser, do I rename all instances of "Total" in your code to my textbox name?
0

LVL 83

Expert Comment

ID: 38775167
No. Just add this at the end

YourTextBoxName.Text = Total
0

Author Comment

ID: 38775252
This is the code I am trying:

Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail

Dim Total = TblPlumbingBidDetail.Compute("Sum(TextBoxEstFiMaterials.text")

Has an error on the underlined...
0

LVL 42

Expert Comment

ID: 38775286
Whats the error?
Did u try both suggestions?
0

LVL 83

Expert Comment

ID: 38775414
I said no to "should i replace total with textbox name"

Dim Total = TblPlumbingBidDetail.Compute("Sum(Total)")

TextBoxEstFiMaterials.text = total
0

LVL 42

Expert Comment

ID: 38775854
that should work:

``````Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail
Dim sum As Long = 0
For Each dr As DataRow In TblPlumbingBidDetail.Rows.Cast(Of DataRow)()
sum += (dr.Field(Of Long)(0) + dr.Field(Of Long)(1)) * dr.Field(Of Long)(2)
Next
Textbox1.Value = sum.ToString()

``````
0

Author Comment

ID: 38776463
Sedgwick:

Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail
Dim sum As Long = 0
For Each dr As DataRow In TblPlumbingBidDetail.Rows.cast(Of DataRow)()
sum += (dr.Field(Of Long)(0) + dr.Field(Of Long)(1)) * dr.Field(Of Long)(2)
Next
Me.TextBoxEstFiMaterials.Text = sum.ToString()

Error on the underlined:
Error      16      'cast' is not a member of 'System.Data.DataRowCollection'.

I can't fix the error. The code must be for a newer vb.net........I am using 2005...
0

Author Comment

ID: 38776468
CodeCruiser:

Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail

Dim Total = TblPlumbingBidDetail.Compute("Sum(Total)")
TextBoxEstFiMaterials.Text = Total

Error on the underlined:

Error      16      Argument not specified for parameter 'filter' of 'Public Function Compute(expression As String, filter As String) As Object'.
0

LVL 83

Expert Comment

ID: 38776503
Change that line to this

Dim Total = TblPlumbingBidDetail.Compute("Sum(Total)", "")
0

Author Comment

ID: 38776599

Can I change "Total" to something else?  It says "A column named 'Total' already belongs to this DataTable." (I tried Table1 with no luck)...

Is the sample code adding a column to the table named "Total" ??
0

LVL 83

Expert Comment

ID: 38776665
Yes you can change it to anything.
0

Author Comment

ID: 38776723
No matter what I change the name to I still get the same error message
"A column named '_______' already belongs to this DataTable."

This happens on the second update to the database...

I know I have about wore this one out - but do you have any ideas on what to do.

Thanks!
0

LVL 42

Assisted Solution

sedgwick earned 100 total points
ID: 38777264
try this:

``````Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail
Dim sum As Long = 0
For Each dr As DataRow In TblPlumbingBidDetail.Rows
sum += (Long.Parse(dr.Item(0).ToString()) + Long.Parse(dr.Item(1).ToString())) * Long.Parse(dr.Item(2).ToString())
Next
Me.TextBoxEstFiMaterials.Text = sum.ToString()
``````
0

LVL 83

Expert Comment

ID: 38777764
Show us the code you tried.
0

Author Comment

ID: 38779224
Sedgwick:

Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail
Dim sum As Long = 0
For Each dr As DataRow In TblPlumbingBidDetail.Rows
Next
Me.TextBoxEstFiMaterials.Text = sum.ToString()

Error: Input string was not in a correct format.
0

Author Comment

ID: 38779238
CodeCruiser:

`````` Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail

Dim Calcs = TblPlumbingBidDetail.Compute("Sum(Calcs)", "")
TextBoxEstFiMaterials.Text = Calcs
``````
0

LVL 83

Accepted Solution

CodeCruiser earned 400 total points
ID: 38779317
And no matter what you use in place of Calcs in this line

you still get error? Are you sure the names you are using do not exist in there already? Are you calling this code repeatedly? If yes then change above code to

``````       Dim TblPlumbingBidDetail As DataTable = Me.MyDataSet.tblPlumbingBidDetail
If TblPlumbingBidDetail.Columns.Contains("Calcs") = False Then
End If

Dim Calcs = TblPlumbingBidDetail.Compute("Sum(Calcs)", "")
TextBoxEstFiMaterials.Text = Calcs
``````
0

Author Comment

ID: 38779781
Ok.......that took  care of the duplication error but I get this number in the textbox:

3064731.93

I tried removing all code that may be a conflict and removing all values in the columns in the total grid and that number still appears on a button click event and form load.
0

LVL 83

Expert Comment

ID: 38779941
And you do not put any text in textbox at design time? Show us all the code you have.
0

Author Comment

ID: 38780650
I'm not entering anything in the textbox...

It's a lot of code...

Can you tell me......is the number appearing because all 3 column data types is money?
0

LVL 83

Expert Comment

ID: 38782180
The number is appearing because it is being put in there. Textbox has nothing to do with grid or datatable so a number in textbox would not appear magically on its own unless above code is being called.
0

Author Comment

ID: 38783464
I will create a new app with no conflicting code and test this afternoon.
0

Author Comment

ID: 38785236
CodeCruiser:

Apoligies...My application was inserting values in the textbox......made up a new simple app and the code worked great.
0

## Featured Post

### Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user aâ€¦
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future â€¦
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files â€” any plâ€¦