Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group on a datatable in a dataset 1

Status
Not open for further replies.

stsuing

Programmer
Aug 22, 2001
596
US
I would like to return an array of rows from datatable. The Array returned should be the datatable rows grouped on certain fields.

Ex
Dataset table
1 3 124
2 3 444
3 3 444
4 3 555
5 4 123
6 4 123

Rows Returned
3 123
4 123
3 444
3 555


Something like
dim dRows() as datarow = _
dataset.tablename.Select(???)
or a dataview of the table.

Any help would be appreciated. I don't want to write an ugly loop
 
Couple of ways.

one was is to use the DataTable's Select method.

You pass it a filter (i.e. "id > 5") and a sorting description (i.e. "id DESC" or "id ASC").

Another way is to dump your datatable into a DataView, and use it's sort method (a little simpler)
DataView1.Sort("id DESC")

Either way may be what you're looking for.

jack
 
I'm not looking to sort the data. I'm looking to Group or select distinct rows on a table. The select method doesn't indicate that's possible.

Thanks anyway
 
ok. Question: why not just select distinct records when you initially fill the datatable from the database? do you need to have all those duplicate records hanging around?

Jack
 
I need all the records because eventually they all make it to a file I'm writing. The structure of the file demand groupings based on those records. But you are correct. I could make another call to the database to get the groupings, but didn't want to.

It seems to me that If I had all the data I needed in a dataset then I should be able to come up with solution without making a call to the database.
 
k, give me a bit more bakground:
-what sort of file are you writing to?
-if all the records are for the file, what is the distinct list of records for? Selection? Display?
-what database are you using?

Jack
 
I am writing a NSF 3.01 file and the detail lines contain information that will determine fields in the batch Header record, the number of batches, and the detail lines in those batches. I am using SQL 2000.

I think I've got a solution.

I will add a table to the dataset that has a column that contains the values that I wanted to group on. Then I will add a relation to the dataset, and then go through the table I added and getchildrows.

That's should work, and is better then crazy loops.

Thanks
 
You could also try this out. Basically you do a select on the data in the dataset and fill up your new datagrid with the new data. I tested this using the Northwinds database and it worked fine both grids I had on the page showed different data.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet1()
ds.Merge(DataSet11.tablename.Select("sortexp"))

DataGrid2.DataSource = ds
DataGrid2.DataMember = "tablename"

DataGrid2.DataBind()

End Sub


HTH That'l do donkey, that'l do
[bravo] Mark
 
stsuing,

Do you have code that you can share for this? I am struggling with the same issue. I opened thread855-686103 which describes this same type of scenario.


Please read thread in this posting before continuing with this paragraph.>>
I'm not sure how I would be able to use the GetChildRows method in my case since I can't get unique email addresses when retrieving data. I do need to get a count of the unique addresses to be displayed in a label on the page. Can you shed any light on this? Can anyone? thanks so much.


regards,
Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top