Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Sorting a Dataset Table

$
0
0

Hi Everyone,

I have a data sorting problem I need help with.

I have a database with several tables.  I have created a dataset containing all of those tables, but only the pertinent info from each table.

I have a combobox that I have bound to a field ("TAGS") in one of the tables ("TAGSTABLE") . The data rows in the table were not entered into the DB in the proper order.

I need to sort the data displayed in the combobox in alphabetical order based on the "TAGS" field.

In addition, I am getting the values from adjacent fields of the selected combobox item using the .SelectedIndexChanged event of the combobox:

strDesc = DataSet.Tables("TAGSTABLE").Rows(strCurRow)("DESCRIPTION")
strPrfx = DataSet.Tables("TAGSTABLE").Rows(strCurRow)("PREFIX")

I understand you cannot change the .Sort property of a combobox if it is bound.

So, I tried using the following code to sort the data in the combobox

BindingSource.Sort = "TAGS"

The field sorted properly in the combobox as expected, but when clicking on a value in the combobox, the incorrect value was selected for the strDesc and strPrfx variables (noted above).  I can only assume that the data is only being sorted in one field?

For giggles I also tried unbinding the combobox and assigning its datasource at runtime and then sorting that, but got the same results.

combobox.DataSource = DataSet.Tables("TAGSTABLE")
combobox.DisplayMember = "TAGS"
BindingSource.Sort = "TAGS"

And as a last resort, I tried creating a new DataView and attempted to sort that with the same results as above.

Dim dSet As DataSet = DataSet
Dim dView As DataView = (dSet.Tables(0).DefaultView)
dView.Sort = "TAGS"
combobox.DataSource = dView

Here is an example of what is in the table:

TAGS DESCRIPTION PREFIX
Tag3 Description3 Prefix3
Tag1 Description1 Prefix1
Tag4 Description4 Prefix4
Tag2 Description2 Prefix2

Here is an example of what I expect to see after picking a combobox value and getting the strDesc and strPrfx values from the .SelectedIndexChanged event:

Tag1 Description1 Prefix1

etc...

So, if I select Tag1 in the combobox, I need to see Description1 in the strDesc variable and Prefix1 in the strPrfx variable

I am currently getting random values.  As my code is currently if I select Tag1 in the combobox, I might get Description4 for strDesc and Prefix4 for strPrfx.

I am stuck! How can I sort the data so all of the data (all row contents) are sorted and not just the values in the sorted field?

Thanks for any help you can offer!


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>