Solved

datagrid problem - sorting

Posted on 2004-09-02
5
251 Views
Last Modified: 2010-04-15
I have a problem with my DataGrid.
Afther I select sorting by anything else then id (which coincides with my default view), i get the new DataGrid layout, but then if i try to do anything with my DataGrid entries, i get wrong IDs.
The IDs I get alre from the old (default) DataGrid.

What am I missing?

code:
private void SortCommand(object source,System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
            {      
                  DataGrid1.DataSource = GetDSbySortExpr(int.Parse(e.SortExpression));
                  DataGrid1.DataBind();
            }

where GetDSbySortExpr connect to the database and returns a query according to my SortExpression:
 switch (SortExpression)
{
   case 0 : select...order by Id..
   case 1 : select...order by name...
}

TNX!
0
Comment
Question by:jurij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Expert Comment

by:hunor_nam
ID: 11962637
Hm... how do you retrive the id-s? I mean from the grid (that's your problem, right?)
And is that the same querry? only different order by?
If so, it would be better to use dataView over the dataSet, so you would sort without going to the db...
but i really don't know you're context... are there many rows? do you HAVE to go to db?
0
 
LVL 10

Assisted Solution

by:123654789987
123654789987 earned 125 total points
ID: 11962656
U have to do a sort of the dataview

private void SortByColumns()
{
   // Get the DefaultViewManager of a DataTable.
   DataView myDataView = DataTable1.DefaultView;
   // By default, the first column sorted ascending.
   myDataView.Sort = "ID DESC";
}

Then rebind the dataview to the datagrid

0
 

Accepted Solution

by:
hunor_nam earned 125 total points
ID: 11962742
Yes exactly, if you dont need to go to the db than this is you're way.

Even more, i would recomend looking at the dataView class as it can easen you're work verry much.
You can even filter (for example) by seting the rowFilter (if i recall) property to a valid t-sql condition.

Please give feedback on what is that you acctually want to do.
0
 

Author Comment

by:jurij
ID: 11964912
Damn. I had a nasty bug in my code, that refilled my DataGrid every time Page_Load fired :/ No wonder i always recived data from default view :]

Tnx for all your help, I'll split the points.

One more thing before i close the thread. What is better.. To get sorted items from Database or to sort items from dataview? It probably depends on the amount of data beeing past from Database (if so, how many items). I'm currently working with less than 20 entries.

Thank you all.


0
 

Expert Comment

by:hunor_nam
ID: 11970721
HI Jurij.
I'm happy to hear you managed to sort things out.
For 20 records it's no use to go to the database every time...
How many it's safe? I actually can't say anything wise there... You should do some checking and see what's better.
Regard,
H.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question