Solved

datagrid problem - sorting

Posted on 2004-09-02
5
245 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now