Solved

datagrid problem - sorting

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

12 Experts available now in Live!

Get 1:1 Help Now