Sorting DataGrid, Maintane Sort

I have made a sorting datagrid which pages fine and maintanes the sort. But the problem is that when I go to a differnt page while one column is being sorted it still has it sorted but the arrow on the header is gone and it goes back to looking like that column is not sorted even tho it really is.

You can see a the Actual page here: http://company4.go-look.com/Liquids.aspx When you click on the headers it displays that arrow but when pages arrow is gone.
[code]
<form runat="server">
<script runat="server" language="VB">
Protected Dim ResultCount As Integer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

If Not Page.IsPostBack Then
BindGrid()
End If
dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

end sub


Sub BindGrid()
Dim cnn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim sql As String

If viewstate("sortfield") Is Nothing Then
sql = "select * from Liquids"
Else
sql = "select * from Liquids order by " & viewstate("sortfield") & " " & viewstate("sortdirection")
End If
cnn = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=D:\Inetpub\wwwcompany4go-look\DB\RaceDayNutrition.mdb")
da = New OleDbDataAdapter(sql, cnn)
da.Fill(ds, "Liquids")

Dim RcdCount As Integer
RcdCount = ds.Tables("Liquids").Rows.Count.ToString()
dtg.PageSize = CInt(ps.SelectedItem.Value)
If Not Page.IsPostBack Then
dtg.CurrentPageIndex = 0
End If
ResultCount = RcdCount
RecordCount.Text = "<b><font color=#999999>" & RcdCount & "</font> records found"
dtg.DataSource = ds
dtg.DataMember = "Liquids"
dtg.DataBind()

If dtg.CurrentPageIndex <> 0 Then
Call Prev_Buttons()
Firstbutton.Visible = True
Prevbutton.Visible = True
Else
Firstbutton.Visible = False
Prevbutton.Visible = False
End If
If dtg.CurrentPageIndex <> (dtg.PageCount - 1) Then
Call Next_Buttons()
Nextbutton.Visible = True
Lastbutton.Visible = True
Else
Nextbutton.Visible = False
Lastbutton.Visible = False
End If
lblPageCount.Text = "Page " & dtg.CurrentPageIndex + 1 & " of " & dtg.PageCount

End Sub

Private Sub Item_Bound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

e.Item.Attributes.Add("onClick", "this.bgColor='#CDFEFC'")

e.Item.Attributes.Add("onMouseOver", "this.bgColor='#EBEBEB'")
e.Item.Attributes.Add("onMouseOut", "this.bgColor='#FFFFFF'")
End If
End Sub

Private Sub DataGrid1_SortCommandByVal(ByVal source As Object, ByVal e As DataGridSortCommandEventArgs)
viewstate.Add("sortfield", e.SortExpression)
dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"
If viewstate("sortdirection") Is Nothing Then
viewstate.Add("sortdirection", "ASC")
End if

If viewstate("sortdirection") = "ASC" Then
viewstate("sortdirection") = "DESC"
Select Case e.SortExpression

Case "Manufacturer_Brand"
dtg.Columns(0).HeaderStyle.CssClass = "sortedDesc"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"
Case "Product_Name"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "sortedDesc"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"
Case "Product_Flavor"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "sortedDesc"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "Calories"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "sortedDesc"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "calories_from_fat"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "sortedDesc"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "Total_fat_grams"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "sortedDesc"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "cholesterol_mg"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "sortedDesc"

End Select
Else
viewstate("sortdirection") = "ASC"
Select Case e.SortExpression

Case "Manufacturer_Brand"
dtg.Columns(0).HeaderStyle.CssClass = "sortedAsc"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"
Case "Product_Name"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "sortedAsc"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"
Case "Product_Flavor"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "sortedAsc"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "Calories"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "sortedAsc"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "calories_from_fat"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "sortedAsc"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "Total_fat_grams"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "sortedAsc"
dtg.Columns(6).HeaderStyle.CssClass = "unsorted"

Case "cholesterol_mg"

dtg.Columns(0).HeaderStyle.CssClass = "unsorted"
dtg.Columns(1).HeaderStyle.CssClass = "unsorted"
dtg.Columns(2).HeaderStyle.CssClass = "unsorted"
dtg.Columns(3).HeaderStyle.CssClass = "unsorted"
dtg.Columns(4).HeaderStyle.CssClass = "unsorted"
dtg.Columns(5).HeaderStyle.CssClass = "unsorted"
dtg.Columns(6).HeaderStyle.CssClass = "sortedAsc"

End Select

End If



BindGrid()
End Sub

Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As DataGridPageChangedEventArgs)
dtg.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub

Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim arg As String = sender.CommandArgument
Select Case arg
Case "next"
If (dtg.CurrentPageIndex < (dtg.PageCount - 1)) Then
dtg.CurrentPageIndex += 1
End If
Case "prev"
If (dtg.CurrentPageIndex > 0) Then
dtg.CurrentPageIndex -= 1
End If
Case "last"
dtg.CurrentPageIndex = (dtg.PageCount - 1)
Case Else
dtg.CurrentPageIndex = Convert.ToInt32(arg)
End Select
BindGrid()

End Sub
Sub Prev_Buttons()
Dim PrevSet As String
If dtg.CurrentPageIndex + 1 <> 1 And ResultCount <> -1 Then
PrevSet = dtg.PageSize
Prevbutton.Text = ("< Prev " & PrevSet)
If dtg.CurrentPageIndex + 1 = dtg.PageCount Then
Firstbutton.Text = ("<< 1st Page")
End If
End If
End Sub

Sub Next_Buttons()
Dim NextSet As String
If dtg.CurrentPageIndex + 1 < dtg.PageCount Then
NextSet = dtg.PageSize
Nextbutton.Text = ("Next " & NextSet & " >")
End If
If dtg.CurrentPageIndex + 1 = dtg.PageCount - 1 Then
Dim EndCount As Integer = ResultCount - (dtg.PageSize * (dtg.CurrentPageIndex + 1))
Nextbutton.Text = ("Next " & EndCount & " >")
End If
End Sub
Sub Repage(ByVal sender As Object, ByVal e As EventArgs)
dtg.CurrentPageIndex = 0
BindGrid()
End Sub
</script>
<style type="text/css">
..unsorted
{
border-bottom:solid 1px #000000;
border-right:solid 1px #999999;
border-left:solid 1px #999999;
border-top:solid 1px #999999;
background-color:#e0e0e0;}
..sortedASC
{
border-bottom:solid 1px #000000;
border-right:solid 1px #cccccc;
border-left:solid 1px #666666;
border-top:solid 1px #666666;
background-color:#FFFFFF;
background-image: url(images/Arrow_Down.bmp);
background-position: center right;
background-repeat:no-repeat;
cursor:hand;
}
..sortedDESC
{
border-bottom:solid 1px #000000;
border-right:solid 1px #cccccc;
border-left:solid 1px #666666;
border-top:solid 1px #666666;
background-color:#FFFFFF;
background-image: url(images/Arrow_Up.bmp);
background-position: center right;
background-repeat:no-repeat;
cursor:hand;
}
..item9
{
background-image: url(images/bg.png);
background-position: center right;
background-repeat:repeat;
cursor:hand;
}

..style1 {font-family: Arial, Helvetica, sans-serif }
..style1 {font-size:26px }
..style3 {
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
color: #666666;
}
..style5 {font-size: 12px; font-family: Arial, Helvetica, sans-serif; }
..style6 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 14px;
}
</style>
[/code]
I am extremely gratefull for any help as this is a work project and due tommrow! Thanks a lot
- JAson


Visit VistaForums.com #1 Windows Vista Forum


0
Jazsun
8/2/2004 6:31:03 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

0 Replies
806 Views

Similar Articles

[PageSpeed] 40
Get it on Google Play
Get it on Apple App Store

Reply:

Similar Artilces:

Datagrid sorting, the Up and Down Triangle sort indicator
When sorting is turned on for a datagrid it turns the headers into simple hyper links that call a function when clicked. It has been requested however that we implement the little triangle icon to indicate whether the sort is ascending or descending. I am almost positive this has had to come up before. Does anyone have a quick and easy solution?Those who say it can not be done, should not interrupt those who are doing it!...

Datagrid sort command not sorting, help!
I have datagrids that populate on linkbutton clicks... when the datagrid loads, the headers need to be sortable.  Usually its pretty easy, but this time its just not working and Im not sure how to figure out why.  I was hoping that if I showed my code, someone could help me figure out why its not executing correctly.  Can someone help?  Here's the datagrid <asp:DataGrid id="dgrNew" runat="server" Width="95%" BorderColor="#666666" BorderWidth="1px" Font-Size="X-Small"        Font-Names="Arial" AllowSorting="True" AutoGenerateC...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

DataGrid sorting
Hi...Right now..I have a nice datagrid with both sorting and paging working fine. Here, I am sorting the results by clicking the header text of the datagrid column. by default, the sorting direction for a datagrid column is Ascending. How can we achive sorting in both directions by the alternate clicks on the header text? Thanks SJ Take a look @ Ascending and Descending Sorting of Columns in an ASP.NET DataGrid... Sushila Bowalekar PatelVisual ASP/ASP.NET MVPhttp://weblogs.asp.net/sushilasb...

DataGrid Sort Command not firing when boundcolumns generated dynamically in a USER CONTROL containing DataGrid
Dear All,I'm developing a USER CONTROL where the datagrid's sortcommand event never fires after dynamically generating bound columns in the datagrid and setting AutoGenerateColumns=false My container page is here:- <%@ Page Trace="true" language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="RNDApplication._default" %><%@Register TagPrefix="MyControl" TagName="DG" src="DG1.ascx"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML><HEAD><title>default</title><meta name="GENERATOR" Content="Microsoft Visual S...

client side sort and server side sort in the same datagrid
Hi,Can i do client side sort and server side sort in the same datagrid? and by parametet decise which sort to activate?Thanks I wouldn't suggest it as a client-side sort is going to be 1. hard to create and implement, 2. almost impossible to retain values across PostBacks, and 3. slow.NC... thanks....

Datagrid sort -Sorting a column which is mapped to database column
How do I sort a datagrid column ,which is not mapped to database column. I'm able to sort other columns from the same datagrid which are mapped to database column and this grid has one of the columns which is not mapped to database column. But need to know how to sort the column which is not mapped to database column.     needtoknow: How do I sort a datagrid column ,which is not mapped to database column. I'm able to sort other columns from the same datagrid which are mapped to database column and this grid has one of the columns which is not mapped to database ...

dw sort and sort
PB 10.2 When you create a datawindow you can choose "rows", "sort". In the SQL there is also a tab page "sort". Probably the tab-page sort in SQL is only when retrieving the data. The tabpage sort is used you call the dw_sort() function. Is this correct? Is the "rows", "sort" called automatically after the data is retrieved? Thanks Eric On 18 Aug 2006 03:24:30 -0700, ontsnapt <ontsnapt@hotmail.com> wrote: >PB 10.2 > >When you create a datawindow you can choose "rows", "sort". In the ...

Problems when sorting in DataGrid control
I got the following problem when i'm sorting DataGrid columns (DataGrid.DataSource=DataView): all the values in grid are sorted as strings regardless of their real data type in underlying data source. For example currency values are ordered like: 100,00 1200,00 20,00 and so on. How can i resolve that problem ? What is the type of the data that's stored in your database- are you storing it as the strings "100" or the integer value 100?  If you're storing the data as strings, you will probably have to manipulate the DataTable after you populate it to have the right values.&nb...

Help with datagrid sort feature. One column not wanting to sort
 Hi I have this statement that sorts the columns in the datagrid. All of the columns sort ASC and DESC correctly except "RequestDate"... I dont know why because I'm sure the iIndex is correct. I checked the field name in the database and thats correct. I just dont know why it wont sort that column, any ideas ?  Dim iIndex As Integer  Select Case ColumnToSort                        Case "ID"        &nb...

Sorting a datagrid both directions and not retreiving the contents each time the sort is performed
hi, I was jst wondering is it possible to sort a datagrid by using click on the headers on the DataGrid. Like if I click one time it sorts to Ascending and one I click again it sorts to descending and ALSO each time it is sorted it does not retreive the data from the database. So the application should retreive the data from the database only ONCE when the form is loaded and not on each sorting. HighOnCodingWanna get high! Yes you can. 1) For the Asc/Desc option, you can use the DataView sorting capabilities. 2) for the "not going into the db" you could use either ...

Sorting numerical data in a datagrid control
We have a datagrid on a webform with a datasource that contains a numerical field. However, the datagrid control insists on returning the data as a string ("abc"). We have set the DataFormatString to "{0:0.00}" but the data still comes our sorted as text (i.e. 87.65 comes out higher in the list than 567.89).So how do we get around this one ?I must say I do like Visual Studio Web Developer 2k5 as a development platform, BUT I wish the documentation was more forthcoming. Its no good having all the features in the world if we can't discover how to put them to good effect.Many other questio...

DataGrid, ArrayList and Sorting of DataGrid
I have a data grid whose data source is ArrayList. How do I sort DataGrid !!! - One simple way is I think by adding passing Sort Expression to Stored Procedure which I am using correct ??? Whats the other way ? Which one is better ?? The other way will depend on the data structure which used to extract the data. Why arraylist? What is contained in the arraylist. Sorting can be done either at the data extraction time (modifying the ORDER BY clause of the query string) or after the extraction of the data(for example, using the DataView object if data extracted is stored in the Data...

Sorting Datagrid Empties the Datagrid
Not sure if this should be put in this forum or the 'Getting Started' forum.. I have a datagrid that fills with a dataset and shows all the rows. When I click on a column header to sort the row the screen 'posts back' and then the datagrid is empty. I think I'm just missing a line or two of code but I'm totally lost so I could be missing a lot... I'm going to paste some code and I hope that's enough, if I should be posting more please let me know... Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If ...

Web resources about - Sorting DataGrid, Maintane Sort - asp.net.presentation-controls

Want to preview our new DataGrid for Xamarin.Forms?
tl;dr Zumero.DataGrid is a Xamarin.Forms control for displaying data in rows and columns. If you would be interested in testing and previewing ...

Mad King DataGrid
This post is about scrolling in WPF and the egocentric DataGrid control. I’ll give the project background but if all you’re interested in is ...

Stack Overflow
Stack Exchange log in - careers - chat - meta - about - faq Questions Tags Users Badges Unanswered Ask Question Top Questions interesting 295 ...

Microsoft Silverlight - Wikipedia, the free encyclopedia
Microsoft Silverlight is an application framework for writing and running rich Internet applications , with features and purposes similar to ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Casaba Security » Products » X5s Testing Tool For Finding XSS Vulnerabilities
Security analysis, consulting, development and testing partners who understand your business strategy.

CodeGuru - Microsoft developers related ideas, articles, tips, tricks, comments, downloads, and so much ...
CodeGuru is where developers can come to share ideas, articles, questions, answers, tips, tricks, comments, downloads, and so much more related ...

Caliburn Micro: WPF, Silverlight, WP7 and WinRT/Metro made easy.
A small, yet powerful framework designed for WPF, Silverlight and WP7. It implements a variety of UI patterns for solving real-world problems. ...

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 11 Jul 2012

license-discuss@opensource.org: 11636: first draft of license proliferation committee report
FYI,the first draft report of the OSI's License Proliferation Committee.To join the OSI's license proliferation discuss email list, send anemail ...

Resources last updated: 12/21/2015 4:42:39 AM