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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting with DataGrid

Status
Not open for further replies.

gflaaten

Programmer
Jan 21, 2002
6
NO

Im trying to sort a datagrid that use a dataview as datasource.

I get to view the data in the dataview, but when i set the
dataview's sort property and re-databind the datagrid, the datagrid comes out empty.

I even tried to use a custom button to sort instead of the regular header links.
The code is very simple it is ;)

DataView1.Sort = e.SortExpression
DataGrid1.DataBind()

-Glenn
 
How many fields do the users need to sort by? If its only one, your best bet might be to hard code the value.

DataView1.Sort = "FieldName DESC/ASC"
DataGrid1.DataBind()

Since you aren't getting data to display at all though, I'd check to make sure that there's data to be sorted.
:)

Are you re-filling your DataView every time the user tries to sort?

Jack
 

Hi there..

I get data in the grid at first, it's when i try to sort the data by clicking the headers. I get the data sorted correctly if i set the dataview's property Sort to a column at first.

I need to sort 4 different columns.

Is there a way to refill the dataview, i do not refill the dataset, because that's what i try to avoid, because of speed.

-glenn
 

Hi there..

I get data in the grid at first, it's when i try to sort the data by clicking the headers. I get the data sorted correctly if i set the dataview's property Sort to a column at first.

I need to sort 4 different columns.

Is there a way to refill the dataview? I do not refill the dataset, because that's what i try to avoid, because of speed.

-glenn
 
Yeah, there's two different ways to fill a dataset without going back to the database.

1. Everytime a sort occurs, all the records in the datagrid are assigned to a dataset created on the fly, which is then assigned to the dataview and then destroyed (the dataset that is). This works really well if you have a datagrid that will have alot of editing going on in it.

2. Once you have your dataset, store it in a session variable. Then, when the sort happens, all you have to do is set your dataview to = the Session("Dataset") variable, and you never have to go back to the db again. The only downside to this method is that if you have a really large dataset, you'll be taking up alot of server resources holding it in memory.

So either way, you're dataview will have to get its data from either the datagrid, or a database.

Let me know if you need anything else.
:)

Jack
 

I filled the Dataset on every sort, and it works now. It takes a little more time.

Thanks.

If i am going to use the sesson version, do i have to set the dataview.table property?..

This is what my code look like now, when it works with refilling the dataset every time.

In the fill/search function:

SqlSelectCommand2.Parameters.Item("@SiteName").Value _
= tbSiteName.Text
cmdListSites.Fill(DsDiskUsage, "Sites")
dgListSites.DataBind()

In the sort command:
dataViewSites.Sort = e.SortExpression
SqlSelectCommand2.Parameters.Item("@SiteName").Value _
= tbSiteName.Text
cmdListSites.Fill(DsDiskUsage, "sp_listsites")
dgListSites.DataBind()

Where do i put my Session variable?...
 
No, you shouldn't have to set the dataview.table property at all. You should be able to just say

DV = Session("DataSet").Tables(0).DefaultView
or, if you're storing the datatable, not the entire dataset
DV = Session("DataTable").DefaultView

To your second question, you should fill the session variable once you've filled your dataset. So after your cmdListSites.Fill(DsDiskUsage, "sp_listsites")
thats when I'd assign it to the session variable.

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top