By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

How to calculate cell data in DataGridView VB Express?

P: 11
Hello,

I would like to calculate cells in DataGridView ( VB Express)

My datagridview1 looks something like this.
--------------------------------------
Row# col1 col2 col3 Col 4
--------------------------------------
Row1 25 13 2
Row2 26 24 4
Row3 17 13 10
Row4 etc etc etc
Row5
etc

Now I want to be able to calculate each cells when I press button1 the datagridview1 should calculate automaticly.
the result will be in col 4
For example: Then the datagridview1 would look like this.


Row# col1 col2 col3 Col 4
--------------------------------------
Row1 25 13 2 24 (25-13*2)
Row2 26 24 4 8 (26-24*8)
Row3 17 13 10 40 (17-13*10)
Row4 etc etc etc etc
Row5
etc
Pls, any ideas and codes would be very helpful.
" Notes"
I am very new to VB Express.
The table have fixed data
I have more than 1000 rows to be calculate
I am not using SQL

Thank you in advance
Feb 6 '11 #1
Share this Question
Share on Google+
5 Replies


P: 8
Hi there, i am struggling with the same issue. Have you been able to sort it out?
Nov 28 '11 #2

P: 5
Hi,

I have just seen your request for help. I hope you still log in to see if you have had a response.

You have not said if your datagridview is data bound or not. As you are still learning, I have assumed that it is not data bound.

One thing to remember is that the datagridview rows and columns are zero based when naming them so they do not start at "1" but "0".

Here is my offering for calculating a single row. You can expand on this to meet your needs.

Public Class Form1

Dim A As Double
Dim B As Double
Dim C As Double
Dim D As Double
Dim Z As Double

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

A = Double.Parse(DataGridView1.Item(0, 0).Value)
B = Double.Parse(DataGridView1.Item(1, 0).Value)
C = Double.Parse(DataGridView1.Item(2, 0).Value)
D = Double.Parse(DataGridView1.Item(3, 0).Value)

Z = A + B + C + D

DataGridView1.Item(4, 0).Value = Z.ToString

End Sub
End Class

Please get back to me if you need more help.

Mike Newman
Jan 9 '12 #3

P: 5
Hi Again,

My previous post allows you to enter the required numbers into the datagridview at run time and click on a button "Button1" to do the calculations.


Mike
Jan 9 '12 #4

P: 8
Hi Mike
Many thanks for your mail. My datagrid is attached to a database. It has been a while since i looked at my program which is working, i think i ended up doing pages of code to do the same thing as i wanted to do with the datagridview. I have a very unique and complicated piece of software, it took my a year and a half to master. Whilst creating it i was studying MCPD (for a absolute greenie it was incredibly hard). But as i went on i implimented what i learnt and in june last year i finished my studies and then another 6 months and i was done. Bit of a overshare hey? hehe. So, would there be a way to do calculations with the datagridview if it is attached to a db?

Kind regards Rome'



@Mike Newman
Jan 11 '12 #5

P: 5
Hi Rome'

As I do not know how you have constructed your database, may I suggest you look at the set of turorials made by Beth Massi of Microsoft. The entire set are available for download here: http://archive.msdn.microsoft.com/vb...x?ReleaseId=16

In these videos she shows you how to create databases and datagrdviews directly from a datasets but not how to perform calculations. When you are familiar with those procedures you can apply my code to the columns that you have created in your database.

Have a look at those and get back to me if you need more help.

Mike
Jan 11 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.