Marcusw
asked on
add total row to bound datagridview
how can i add a total row to the bottom of my datagridview.
my dgv is bound to a datatable in a dataset. what i would like to to append a total row to the grid. no updates need to be made to the actual data.
it does need to be on the bottom of the dgv though as that data is often exported directly into excel, and hopefully soon i will be exporting in to word as well
my dgv is bound to a datatable in a dataset. what i would like to to append a total row to the grid. no updates need to be made to the actual data.
it does need to be on the bottom of the dgv though as that data is often exported directly into excel, and hopefully soon i will be exporting in to word as well
ASKER
thanks for the code but i now get this error
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
sorry , the code for inserting into the datagridview was for a unbounded grid
try this
Dim dr As DataRow = dsSale.Tables("Sales").New Row
'Put values into row
dr(0) = ""
dr(1) = "total ="
dr(2) = myvalue
dr(3) = "$"
'and so on, making sure you put values in any ...
'... fields/columns that do not allow null
dsSale.Tables("Sales").Row s.Add(dr)
vbturbo
try this
Dim dr As DataRow = dsSale.Tables("Sales").New
'Put values into row
dr(0) = ""
dr(1) = "total ="
dr(2) = myvalue
dr(3) = "$"
'and so on, making sure you put values in any ...
'... fields/columns that do not allow null
dsSale.Tables("Sales").Row
vbturbo
ASKER
i have had to alter your code slightly as the number of columns and the names of the column change every time the datatable is filled. so i have tired this
Dim drw As DataRow = ds.Tables("Grid").NewRow
'Put values into row
drw(0) = ""
For i As Integer = 1 To drw.Table.Columns.Count
drw(i) = ds.Tables("Grid").Compute( "Sum(" & i & ")", String.Empty)
Next
but now i get the following error
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.
Dim drw As DataRow = ds.Tables("Grid").NewRow
'Put values into row
drw(0) = ""
For i As Integer = 1 To drw.Table.Columns.Count
drw(i) = ds.Tables("Grid").Compute(
Next
but now i get the following error
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.
ASKER
the only thing that remains the same is that the first column is a description column
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i have altered my code to this, so i now use your myvalue variable
Dim drw As DataRow = ds.Tables("Grid").NewRow
Dim myvalue As Object
Dim cnt As Integer = 0
For Each c As DataColumn In ds.Tables("Grid").Columns
If c.ColumnName <> cmbRows.Text Then
myvalue = ds.Tables("Grid").Compute( "Sum(" & c.ColumnName & ")", "")
drw(cnt) = myvalue 'ds.Tables("Grid").Compute ("Sum(" + c.ColumnName + ")", String.Empty)
myvalue = Nothing
cnt = cnt + 1
Else
drw(cnt) = "TOTAL"
cnt = cnt + 1
End If
Next
ds.Tables("Grid").Rows.Add (drw)
and it adds my TOTAL column correctly but brings up an error for the next field
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.
Dim drw As DataRow = ds.Tables("Grid").NewRow
Dim myvalue As Object
Dim cnt As Integer = 0
For Each c As DataColumn In ds.Tables("Grid").Columns
If c.ColumnName <> cmbRows.Text Then
myvalue = ds.Tables("Grid").Compute(
drw(cnt) = myvalue 'ds.Tables("Grid").Compute
myvalue = Nothing
cnt = cnt + 1
Else
drw(cnt) = "TOTAL"
cnt = cnt + 1
End If
Next
ds.Tables("Grid").Rows.Add
and it adds my TOTAL column correctly but brings up an error for the next field
Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bad copy and paste. For
Dim s As String = ("Sum(" & c.ColumnName & ")"
read
Dim s As String = "Sum(" & c.ColumnName & ")"
Roger
Dim s As String = ("Sum(" & c.ColumnName & ")"
read
Dim s As String = "Sum(" & c.ColumnName & ")"
Roger
I see you'e accepted my anser and given me all the points.
That looks a bit unfair to me. The basic answer that you have used was vbturbo's, not mine. I just corrected an error that you made in translating what vbturbo gave you into your own code.
I shall not object if you wish to have this question re-opened so that you can re-allocate the points. Indeed, I shan't object if you do that and give all of them to vbturbo.
Roger
That looks a bit unfair to me. The basic answer that you have used was vbturbo's, not mine. I just corrected an error that you made in translating what vbturbo gave you into your own code.
I shall not object if you wish to have this question re-opened so that you can re-allocate the points. Indeed, I shan't object if you do that and give all of them to vbturbo.
Roger
Hi Roger
No problem at all regarding the assigning the points to you. -:)
Jens
No problem at all regarding the assigning the points to you. -:)
Jens
Jens
Long time no speak.
We'll have to leave it in Marcusw's hands but, if he doesn't change it, I'll owe you one ;-)
Roger
Long time no speak.
We'll have to leave it in Marcusw's hands but, if he doesn't change it, I'll owe you one ;-)
Roger
you can use the DataColumn.Expression Property which also provide aggregation and sum's collumns
http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
Or
dim myvalue as Object
myvalue = dsSale.Tables("Sales").Com
DataGridView1.Rows.Add(Col
vbturbo