473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sum column of a DataGridVuew

I'm trying to write a procedure that sums the values of a column in a
datagridview. If a row is empty (has no value entered) the procedure
crashes. I've added code to check to see if the column for the row is blank,
this error apears:

Conversion from type 'DBNull' to type 'Integer' is not valid.

The code that I've written is: (the variables are defined)

Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
varCol = dgvTblService.CurrentCell.ColumnIndex
varRow = dgvTblService.CurrentCell.RowIndex

If IsDBNull(Me.dgvTblService.CurrentRow.Cells(5)) Then
Me.dgvTblService.CurrentRow.Cells(5).Value =
Convert.ToDouble(0)
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
Else
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
End If
Loop

Any suggestions on what I'm doing wrong?
--
Tony
Oct 30 '06 #1
2 1968
Tony,

Have a look at the datacolumn.compute statement, much easier in my idea

http://msdn2.microsoft.com/en-us/lib...e.compute.aspx

I hope this helps,

Cor

"Tony A." <To***@discussions.microsoft.comschreef in bericht
news:C6**********************************@microsof t.com...
I'm trying to write a procedure that sums the values of a column in a
datagridview. If a row is empty (has no value entered) the procedure
crashes. I've added code to check to see if the column for the row is
blank,
this error apears:

Conversion from type 'DBNull' to type 'Integer' is not valid.

The code that I've written is: (the variables are defined)

Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
varCol = dgvTblService.CurrentCell.ColumnIndex
varRow = dgvTblService.CurrentCell.RowIndex

If IsDBNull(Me.dgvTblService.CurrentRow.Cells(5)) Then
Me.dgvTblService.CurrentRow.Cells(5).Value =
Convert.ToDouble(0)
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
Else
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
End If
Loop

Any suggestions on what I'm doing wrong?
--
Tony

Oct 30 '06 #2
It seems like you do the check on column 5 but sum column 6.

//Christer

"Tony A." <To***@discussions.microsoft.comwrote in message
news:C6**********************************@microsof t.com...
I'm trying to write a procedure that sums the values of a column in a
datagridview. If a row is empty (has no value entered) the procedure
crashes. I've added code to check to see if the column for the row is
blank,
this error apears:

Conversion from type 'DBNull' to type 'Integer' is not valid.

The code that I've written is: (the variables are defined)

Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
varCol = dgvTblService.CurrentCell.ColumnIndex
varRow = dgvTblService.CurrentCell.RowIndex

If IsDBNull(Me.dgvTblService.CurrentRow.Cells(5)) Then
Me.dgvTblService.CurrentRow.Cells(5).Value =
Convert.ToDouble(0)
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
Else
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
End If
Loop

Any suggestions on what I'm doing wrong?
--
Tony

Oct 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
4
by: perspolis | last post by:
I have 3 columns in my datatabel name Total,unit,Price. I use a column expression in my project..and in this expression i multiplied two column... for example ...
6
by: Robert Schuldenfrei | last post by:
Dear NG, After being away from C# programming for a spell, I am trying my hand at what should be a simple task. I have been hitting my head against the wall this morning. I have a simple order...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
6
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
2
by: ricky | last post by:
Hello, If anyone could help me with this I would highly appreciate it. I've tried everything and nothing works. What I am trying to do is so damn basic and it's just frustrating that it seems...
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
4
by: Steph. | last post by:
I have a List view displaying data in Detail mode with several columns. How I can get the column index the user clicked on ? (when user click on an item inside the ListView, not on a column...
4
by: Peter Gibbs | last post by:
I need some help with this problem. I'm using Access 2002 with XP. My problem is with a 2-column listbox. My VBA code puts text data into the listbox. The problem is that the text data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.