Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

datagrid problem - sorting

Posted on 2004-09-02
5
Medium Priority
?
254 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 375 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 375 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview

963 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