473,387 Members | 1,757 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,387 software developers and data experts.

Inserting subtotal rows into a datatable

I have an untyped datatable that has financial numbers and controls that
were populated by code (not a simple fill from a DA). Now I want to insert
subtotals into it. I wrote a sub to do so that uses InsertAt(myrow,i).
When I trace through the sub it is doing the insertat and the value of i is
correct. But when I bind my dataset to a datagrid all of my inserted rows
are at the end? code is below.

What am I doing wrong?
thanks,
G

Private Sub InsertSubtotals()
'spin through the datatable and insert subtotal records
Dim col1Sub As Decimal
Dim col1Fin As Decimal
Dim col2Sub As Decimal
Dim col2Fin As Decimal
Dim V1Sub As Decimal
Dim V1Fin As Decimal
Dim col3Sub As Decimal
Dim col3Fin As Decimal
Dim col4Sub As Decimal
Dim col4Fin As Decimal
Dim V2Sub As Decimal
Dim V2Fin As Decimal
Dim i As Integer
Dim PriorMajCat As String
Dim myCount As Integer = dsDisplay.Tables(0).Rows.Count - 1
For i = 0 To dsDisplay.Tables(0).Rows.Count - 1
Dim row As DataRow = dsDisplay.Tables(0).Rows(i)
If i = 0 Then
PriorMajCat = row("MajCat")
End If
If row("MajCat") <> PriorMajCat Then
Dim newRow As DataRow = dsDisplay.Tables(0).NewRow()
newRow("MajCat") = "TOT" & Val(i) & PriorMajCat
newRow("Account") = "Subtotal " & PriorMajCat
newRow("Col1") = col1Sub
newRow("Col2") = col2Sub
newRow("V1") = V1Sub
newRow("Col3") = col3Sub
newRow("Col4") = col4Sub
newRow("V2") = V2Sub
newRow("ShowFilter") = "show"
dsDisplay.Tables(0).Rows.InsertAt(newRow, i) 'I realize
that I need to adjust i
col1Sub = 0
col2Sub = 0
V1Sub = 0
col3Sub = 0
col4Sub = 0
V2Sub = 0
PriorMajCat = row("MajCat")
Else
'add to subtotals
col1Sub = col1Sub + row("Col1")
col2Sub = col2Sub + row("Col2")
V1Sub = V1Sub + row("V1")
col3Sub = col3Sub + row("Col3")
col4Sub = col4Sub + row("Col4")
V2Sub = V2Sub + row("V2")
'add to final totals
col1Fin = col1Fin + row("Col1")
col2Fin = col2Fin + row("Col2")
V1Fin = V1Fin + row("V1")
col3Fin = col3Fin + row("Col3")
col4Fin = col4Fin + row("Col4")
V2Fin = V2Fin + row("V2")

End If
Next
'insert last subtotal
Dim lastRow As DataRow = dsDisplay.Tables(0).NewRow()
lastRow("MajCat") = "TOT" & PriorMajCat
lastRow("Account") = "Subtotal " & PriorMajCat
lastRow("Col1") = col1Sub
lastRow("Col2") = col2Sub
lastRow("V1") = V1Sub
lastRow("Col3") = col3Sub
lastRow("Col4") = col4Sub
lastRow("V2") = V2Sub
lastRow("ShowFilter") = "show"
dsDisplay.Tables(0).Rows.Add(lastRow)

End Sub
Nov 18 '05 #1
7 3898
"GaryB" <gb@nospam.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
I have an untyped datatable that has financial numbers and controls that
were populated by code (not a simple fill from a DA). Now I want to insert
subtotals into it. I wrote a sub to do so that uses InsertAt(myrow,i).
When I trace through the sub it is doing the insertat and the value of i is
correct. But when I bind my dataset to a datagrid all of my inserted rows
are at the end? code is below.

What am I doing wrong?


How did you set the sort order for your DataTable? How did you set it for
your DataGrid?

John Saunders
Nov 18 '05 #2
There is no "SortOrder" property for either a datagrid or a datatable that I
can find.

the rows appear in the sequence they were added which happens to be the
sequence they were in from another dataset.
G

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
"GaryB" <gb@nospam.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
I have an untyped datatable that has financial numbers and controls that
were populated by code (not a simple fill from a DA). Now I want to
insert subtotals into it. I wrote a sub to do so that uses
InsertAt(myrow,i). When I trace through the sub it is doing the insertat
and the value of i is correct. But when I bind my dataset to a datagrid
all of my inserted rows are at the end? code is below.

What am I doing wrong?


How did you set the sort order for your DataTable? How did you set it for
your DataGrid?

John Saunders

Nov 18 '05 #3
Hi Gary,

From your description, you're using the InsertAt method of
DataRowCollection to insert new rows into a manually generated DataTable
and then bind it onto a webform datagrid. However, you found the new added
rows always display at the bottom of the datagrid, yes?

As John has mentioned, the means how to bind the datatable to the grid may
also change the display order since Only the raw "Rows" collection of the
DataTable will reflect the correct rows order in the collection. If we use
some other methods such as Select() or GetChildRows to retreive datarows
from DataTable, the retuned rows may not represent the actual order in the
Rows collection. So would you provide some info on how you bind the
DataTable onto the grid? Also, I suggest you manually use
for(i=0;i<Table.Rows.Count; i++)
....

to printout all the rows in the DataTable after you inserting new rows to
verify whether the actual order in the Rows collection is correct.

In addition, from the code you provided, seems you will insert new rows
in the following loop

For i = 0 To dsDisplay.Tables(0).Rows.Count - 1
Dim row As DataRow = dsDisplay.Tables(0).Rows(i)

That may cause the Table's Rows Collection be modified during the for loop
and thus the "i" iterate index may point to the incorrect record. Not sure
whether this will also be the potential cause of this issue.

Hope helps. If you have any other findings , please also feel free to post
here. thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4
"GaryB" <gb@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
There is no "SortOrder" property for either a datagrid or a datatable that
I can find.

the rows appear in the sequence they were added which happens to be the
sequence they were in from another dataset.
This sounds like the answer to your question. Your total rows were added
last, so they show up at the bottom.

If you want to impose a sort order, then consider using a DataView and
binding to the DataView and not to the DataSet.

John Saunders

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
"GaryB" <gb@nospam.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
I have an untyped datatable that has financial numbers and controls that
were populated by code (not a simple fill from a DA). Now I want to
insert subtotals into it. I wrote a sub to do so that uses
InsertAt(myrow,i). When I trace through the sub it is doing the insertat
and the value of i is correct. But when I bind my dataset to a datagrid
all of my inserted rows are at the end? code is below.

What am I doing wrong?


How did you set the sort order for your DataTable? How did you set it for
your DataGrid?

John Saunders


Nov 18 '05 #5
Sorry for the distraction. My code was working fine. I was just looking at
output after an additional databind.
G

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:u3**************@tk2msftngp13.phx.gbl...
"GaryB" <gb@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
There is no "SortOrder" property for either a datagrid or a datatable
that I can find.

the rows appear in the sequence they were added which happens to be the
sequence they were in from another dataset.


This sounds like the answer to your question. Your total rows were added
last, so they show up at the bottom.

If you want to impose a sort order, then consider using a DataView and
binding to the DataView and not to the DataSet.

John Saunders

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
news:OO**************@TK2MSFTNGP10.phx.gbl...
"GaryB" <gb@nospam.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
I have an untyped datatable that has financial numbers and controls that
were populated by code (not a simple fill from a DA). Now I want to
insert subtotals into it. I wrote a sub to do so that uses
InsertAt(myrow,i). When I trace through the sub it is doing the insertat
and the value of i is correct. But when I bind my dataset to a datagrid
all of my inserted rows are at the end? code is below.

What am I doing wrong?

How did you set the sort order for your DataTable? How did you set it
for your DataGrid?

John Saunders



Nov 18 '05 #6
Sorry for wasting your time. it was a bogus issue. The code was working
fine - I was just looking at the wrong output.
Gary

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:2F**************@cpmsftngxa10.phx.gbl...
Hi Gary,

From your description, you're using the InsertAt method of
DataRowCollection to insert new rows into a manually generated DataTable
and then bind it onto a webform datagrid. However, you found the new added
rows always display at the bottom of the datagrid, yes?

As John has mentioned, the means how to bind the datatable to the grid may
also change the display order since Only the raw "Rows" collection of the
DataTable will reflect the correct rows order in the collection. If we use
some other methods such as Select() or GetChildRows to retreive datarows
from DataTable, the retuned rows may not represent the actual order in the
Rows collection. So would you provide some info on how you bind the
DataTable onto the grid? Also, I suggest you manually use
for(i=0;i<Table.Rows.Count; i++)
...

to printout all the rows in the DataTable after you inserting new rows to
verify whether the actual order in the Rows collection is correct.

In addition, from the code you provided, seems you will insert new rows
in the following loop

For i = 0 To dsDisplay.Tables(0).Rows.Count - 1
Dim row As DataRow = dsDisplay.Tables(0).Rows(i)

That may cause the Table's Rows Collection be modified during the for
loop
and thus the "i" iterate index may point to the incorrect record. Not sure
whether this will also be the potential cause of this issue.

Hope helps. If you have any other findings , please also feel free to post
here. thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7
Hi GaryB,

Thanks for your followup and glad that the problem is figured out. Have a
nice day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #8

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

Similar topics

23
by: Eva | last post by:
Hi i am trying to insert a new row into one of my datatabels that i have in my dataset when a button is clicked. here is my code Dim ClientInsRow As DataRow = dtClient.NewRo ...
4
by: Deepankar | last post by:
Hi, I was trying to change an example for SQL Server to work with Access db to insert image data. I have everything working except getting the OleDbParameter type for the image column. The...
0
by: a | last post by:
I've read and split a delimited text file into a dataset. It looks fine in a datagrid (5 columns and 5,000 rows), but I've been trying, without success, to then insert the resulting dataset called...
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
6
by: Pushpendra Vats | last post by:
Hi , I am trying to insert records into database. I am trying to write the following code. On button1 click event i am inserting five records and after that i am calling the update method of...
3
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be...
6
by: Manikandan | last post by:
Hi, I need to insert the datetime with milliseconds value into a datarow. My code as below DataTable testDataTable=new DataTable(); testDataTable.Columns.Add("updatedDateTime",...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
3
by: joel | last post by:
Hi guys, Im new to vb.net and i have this problem how do i insert another row into my datatable... (i guess it's like creating a for loop or something i really have no idea)... in this code i...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.