Connecting Tech Pros Worldwide Forums | Help | Site Map

Sort Dataset without binding to datagrid

Newbie
 
Join Date: Mar 2009
Location: Kuala Lumpur
Posts: 2
#1: Mar 16 '09
I have read the following topic:
sorting dataset
and I am facing the same issue.

I am designing a report. I have fill a dataset with the results from a query. From the results, I used to set the value from the items into an array and display on the aspx page in a table form. The reason why I don't use Datagrid or Gridview is because the table has to be design where the column headers has 2 or more rows and some columns need to be merged using colspan and rowspan. The HTML table also can provide me with the hover function for Alternate Text which is dynamic change for each column and rows. ASP.NET controls are lack in these features as I know. I also have linkbutton inside a cell with another static label.

My problem here is that I want to sort the rows of records from the table. My idea is to add linkbutton for the column header title so when click, the rows will be sorted as Ascending or Descending.

I have at least 7 columns to be sorted. If I have to modify the stored procedure then I need to pass in 7 parameters together with the Sort Order (Asc/Desc) for each parameter which will make the stored procedure become long and complicated. I have tried DefaultView property but the dataset is not sorted.

Expand|Select|Wrap|Line Numbers
  1.     Protected Sub lnkSortRegion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSortRegion.Click
  2.         SetParams()
  3.         GetDataset()
  4.         If lnkSortRegion.Text = "A-Z" Then
  5.             dataset1.Tables(0).DefaultView.Sort = "Region"
  6.             lnkSortRegion.Text = "Z-A"
  7.         Else
  8.             dataset1.Tables(0).DefaultView.Sort = "Region Desc"
  9.             lnkSortRegion.Text = "A-Z"
  10.         End If
  11.         PopulateDataToArray()
  12.     End Sub
Expert there please help me. Thanks.

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: Mar 16 '09

re: Sort Dataset without binding to datagrid


The Table.DefaultView property returns a DataView associated with the table.

It can be used for sorting, filtering, searching, editing, and navigation. When you do sorting, filtering, or editing on the DataView, it has no effect on the DataTable it is associated with. This lets you create multiple views of the DataTable without modifying the source.

This means that when you do your sorting and call your PopulateDataToArray() method, you need to use the DataView instead of the DataTable in your DataSet.

I'd recommend modifying your PopulateDataToArray() method so that it accepts a parameter: the data source that should be displayed.

-Frinny
Reply

Tags
array, dataset, sort, table