Datagrid won't sort

D

DC Gringo

I have a datagrid that won't sort. The event handler is firing and return
label text, just not the sort.

Here's my Sub Page_Load and Sub DataGrid1_SortCommand:

--------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
SqlDataAdapter1.Fill(DsAdvanced1)

If Not IsPostBack Then
DataGrid1.DataBind()
Provinces.DataBind()
Districts.DataBind()
SubDistricts.DataBind()
Communities.DataBind()
End If

End Sub

--------------------


Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand

lblWhatClicked.Text = "You clicked: " & e.SortExpression

Dim dv As New DataView(DsAdvanced1.Tables(0))
dv.Sort = e.SortExpression
DataGrid1.DataSource = dv
DataGrid1.DataBind()

End Sub
 
G

Guest

Try changing the followin
dv.Sort = e.SortExpressio

t
dv.Sort = e.SortExpression & " ASC

The way you are doing sort will only handle one way sort (i.e. all columns can only be sorted in ascending order). If you want to be able to sort a column both ascending and descending order you'll need to do some more work

The following is a good article on customizing the DataGrid control. There is a section on how you can implement 2 way sorting (search for What about Sorting? in the article)

http://msdn.microsoft.com/msdnmag/issues/01/10/cutting/default.asp

HTH
Suresh

----- DC Gringo wrote: ----

I have a datagrid that won't sort. The event handler is firing and retur
label text, just not the sort

Here's my Sub Page_Load and Sub DataGrid1_SortCommand

-------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e A
System.EventArgs) Handles MyBase.Loa
'Put user code to initialize the page her
SqlDataAdapter1.Fill(DsAdvanced1

If Not IsPostBack The
DataGrid1.DataBind(
Provinces.DataBind(
Districts.DataBind(
SubDistricts.DataBind(
Communities.DataBind(
End I

End Su

--------------------


Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e A
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handle
DataGrid1.SortComman

lblWhatClicked.Text = "You clicked: " & e.SortExpressio

Dim dv As New DataView(DsAdvanced1.Tables(0)
dv.Sort = e.SortExpressio
DataGrid1.DataSource = d
DataGrid1.DataBind(

End Su
 
D

DC Gringo

Suresh...thank you for assisting. I added the & " ASC" as you suggested.
Unfortunately, that did not change anything...the datagrid still not
sorting...it is posting and running the Sub as evidenced by my
lblWhatClicked.Text = "You clicked: " & e.SortExpression, but the table is
still un-sorted.

Could you take another look?

Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand

lblWhatClicked.Text = "You clicked: " & e.SortExpression

Dim dv As New DataView(DsAdvanced1.Tables(0))
dv.Sort = e.SortExpression & " ASC"
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

SqlDataAdapter1.Fill(DsAdvanced1)
If Not IsPostBack Then
DataGrid1.DataBind()
Provinces.DataBind()
Districts.DataBind()
SubDistricts.DataBind()
Communities.DataBind()

End If
End Sub

Suresh said:
Try changing the following
dv.Sort = e.SortExpression

to
dv.Sort = e.SortExpression & " ASC"

The way you are doing sort will only handle one way sort (i.e. all columns
can only be sorted in ascending order). If you want to be able to sort a
column both ascending and descending order you'll need to do some more work.
The following is a good article on customizing the DataGrid control.
There is a section on how you can implement 2 way sorting (search for What
about Sorting? in the article).
 
D

DC Gringo

Suresh, thanks for the follow-up...

When I tried that, I get an error on compile: "Value of type
'System.Data.DataView' cannot be converted to 'System.Data.DataTable'."

_____
DC G
 
G

Guest

Sorry for that I made a mistake. I'm not familiar with VB.NET that much

So
Dim dv As New DataView(DsAdvanced1.Tables(0).DefaultView
shoule b
Dim dv As DataView = ds.Tables("Books").DefaultVie

Just FYI (I'm posting VB.NET code with a grid that has sort enabled and working

ASPX
------
<asp:DataGrid id="DataGrid1" runat="server" AllowSorting="True"></asp:DataGrid

VB Codebehind
-----------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
If Not Page.IsPostBack The
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd"
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select title_id, title, price from Titles", objConn

Dim ds As DataSet = New DataSet(
objAdap.Fill(ds, "Books"

Dim dv As DataView = ds.Tables("Books").DefaultVie

dv.Sort = "title ASC
DataGrid1.DataSource = ds.Tables("Books").DefaultVie
DataGrid1.DataBind(
End I
End Su

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortComman
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd"
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select title_id, title, price from Titles", objConn

Dim ds As DataSet = New DataSet(
objAdap.Fill(ds, "Books"

Dim dv As DataView = ds.Tables("Books").DefaultVie

dv.Sort = e.SortExpression & " ASC
DataGrid1.DataSource = ds.Tables("Books").DefaultVie
DataGrid1.DataBind(
End Su

Hope this helps you narrow the problem
Suresh

----- DC Gringo wrote: ----

Suresh, thanks for the follow-up..

When I tried that, I get an error on compile: "Value of typ
'System.Data.DataView' cannot be converted to 'System.Data.DataTable'.

____
DC
 
D

DC Gringo

Suresh,

Seems like we're almost there. I realized I had a SORT BY ASC command in my
stored procedure that may have been hindering. I still unable, however, to
get either the pre-are postback code to sort in either direction. To
simplify and troubleshoot, I'm just try to sort ASC by a specified field in
the the initial load of the page, and then sort DESC by the same column in
the event of a DataGrid1_SortCommand.

No sorting is taking place at all with what I have here, though.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then
SqlDataAdapter1.Fill(DsAdvanced1)
Dim dv2 As DataView =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
dv2.Sort = " clnName ASC"
'e.SortExpression &
DataGrid1.DataSource =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
DataGrid1.DataBind()

Provinces.DataBind()
Districts.DataBind()
SubDistricts.DataBind()
Communities.DataBind()

End If

End Sub

Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand

lblWhatClicked.Text = "You clicked: " & e.SortExpression

SqlDataAdapter1.Fill(DsAdvanced1)
Dim dv2 As DataView =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
dv2.Sort = " clnName DESC"
'e.SortExpression &
DataGrid1.DataSource =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
DataGrid1.DataBind()
End Sub


_____
DC G

Suresh said:
Sorry for that I made a mistake. I'm not familiar with VB.NET that much.

So
Dim dv As New DataView(DsAdvanced1.Tables(0).DefaultView)
shoule be
Dim dv As DataView = ds.Tables("Books").DefaultView

Just FYI (I'm posting VB.NET code with a grid that has sort enabled and working)

ASPX:
AllowSorting="True"> said:
VB Codebehind:
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd")
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select
title_id, title, price from Titles", objConn)
Dim ds As DataSet = New DataSet()
objAdap.Fill(ds, "Books")

Dim dv As DataView = ds.Tables("Books").DefaultView

dv.Sort = "title ASC"
DataGrid1.DataSource = ds.Tables("Books").DefaultView
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd")
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select title_id,
title, price from Titles", objConn)
 
G

Guest

I realized I had a SORT BY ASC command in my stored procedure that may have been hindering
No this is ok. Because when you sort the DataView it's already detached from the DB and does a fresh sort on the recordset with the criteria specified by the user (i.e. clicking on the columns)

Damn, again another mistake by me. Although it does work either way in my sample
DataGrid1.DataSource = DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie

Instead shoule b
DataGrid1.DataSource = d

Finally in the DataGrid1_SortCommand method
Dim dv2 As DataView = DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie
dv2.Sort = " clnName DESC
'e.SortExpression &>DataGrid1.DataSource = DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie
DataGrid1.DataBind(

dv2.Sort = e.SortExpression & "DESC
o
dv2.Sort = e.SortExpression & "ASC

You have the following
dv2.Sort = " clnName DESC
This will always sort by clnName DESC everytime you click on a column header

Change it to the way I've described above and see if it'll work
Suresh

----- DC Gringo wrote: ----

Suresh

Seems like we're almost there. I realized I had a SORT BY ASC command in m
stored procedure that may have been hindering. I still unable, however, t
get either the pre-are postback code to sort in either direction. T
simplify and troubleshoot, I'm just try to sort ASC by a specified field i
the the initial load of the page, and then sort DESC by the same column i
the event of a DataGrid1_SortCommand

No sorting is taking place at all with what I have here, though

Private Sub Page_Load(ByVal sender As System.Object, ByVal e A
System.EventArgs) Handles MyBase.Loa
'Put user code to initialize the page her

If Not Page.IsPostBack The
SqlDataAdapter1.Fill(DsAdvanced1
Dim dv2 As DataView
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie
dv2.Sort = " clnName ASC
'e.SortExpression
DataGrid1.DataSource
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie
DataGrid1.DataBind(

Provinces.DataBind(
Districts.DataBind(
SubDistricts.DataBind(
Communities.DataBind(

End I

End Su

Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e A
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handle
DataGrid1.SortComman

lblWhatClicked.Text = "You clicked: " & e.SortExpressio

SqlDataAdapter1.Fill(DsAdvanced1
Dim dv2 As DataView
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie
dv2.Sort = " clnName DESC
'e.SortExpression
DataGrid1.DataSource
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie
DataGrid1.DataBind(
End Su


____
DC

Suresh said:
Sorry for that I made a mistake. I'm not familiar with VB.NET that much
Dim dv As New DataView(DsAdvanced1.Tables(0).DefaultView
shoule b
Dim dv As DataView = ds.Tables("Books").DefaultVie
[/QUOTE]
System.EventArgs) Handles MyBase.Loa
If Not Page.IsPostBack The
Dim objConn As SqlConnection = Ne SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd"
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("selec
title_id, title, price from Titles", objConn
DataGrid1.DataSource = ds.Tables("Books").DefaultView
DataGrid1.DataBind()
End If
End Sub
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd")
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select title_id,
title, price from Titles", objConn)
 
D

DC Gringo

Suresh,

Nope, still same thing.

Should I have a special "Imports" statement at the top of this user
control's code-behind? Currently I don't have any.

I'm using SqlDataAdapter remember. Although I'm assuming that if there was
a problem with Imports, it would not even render what data I have.

_____
DC G

Suresh said:
have been hindering.
No this is ok. Because when you sort the DataView it's already detached
from the DB and does a fresh sort on the recordset with the criteria
specified by the user (i.e. clicking on the columns).
Damn, again another mistake by me. Although it does work either way in my sample. DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView

Instead shoule be
DataGrid1.DataSource = dv

Finally in the DataGrid1_SortCommand method.


dv2.Sort = e.SortExpression & "DESC"
or
dv2.Sort = e.SortExpression & "ASC"

You have the following,
This will always sort by clnName DESC everytime you click on a column header.

Change it to the way I've described above and see if it'll work.
Suresh.

----- DC Gringo wrote: -----

Suresh,

Seems like we're almost there. I realized I had a SORT BY ASC command in my
stored procedure that may have been hindering. I still unable, however, to
get either the pre-are postback code to sort in either direction. To
simplify and troubleshoot, I'm just try to sort ASC by a specified field in
the the initial load of the page, and then sort DESC by the same column in
the event of a DataGrid1_SortCommand.

No sorting is taking place at all with what I have here, though.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If Not Page.IsPostBack Then
SqlDataAdapter1.Fill(DsAdvanced1)
Dim dv2 As DataView =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
dv2.Sort = " clnName ASC"
'e.SortExpression &
DataGrid1.DataSource =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
DataGrid1.DataBind()

Provinces.DataBind()
Districts.DataBind()
SubDistricts.DataBind()
Communities.DataBind()

End If

End Sub

Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand

lblWhatClicked.Text = "You clicked: " & e.SortExpression

SqlDataAdapter1.Fill(DsAdvanced1)
Dim dv2 As DataView =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
dv2.Sort = " clnName DESC"
'e.SortExpression &
DataGrid1.DataSource =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultView
DataGrid1.DataBind()
End Sub


_____
DC G

enabled and
working)
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim objConn As SqlConnection = New
SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd")
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select
title_id, title, price from Titles", objConn)
DataGrid1.DataSource = ds.Tables("Books").DefaultView
DataGrid1.DataBind()
End If
End Sub
As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
Dim objConn As SqlConnection = New
SqlConnection("database=pubs;server=mysqlserver;uid=myuname;password=mypwd")
Dim objAdap As SqlDataAdapter = New SqlDataAdapter("select
title_id,
title, price from Titles", objConn)
DataGrid1.DataSource = ds.Tables("Books").DefaultView
DataGrid1.DataBind()
End Sub
Suresh.
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top