473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Datagrid won't sort

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.DataGridSortCommandEvent Args) 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

--
_____
DC G
Nov 18 '05 #1
7 2419
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/is...ng/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.DataGridSortCommandEvent Args) 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

--
____
DC

Nov 18 '05 #2
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.DataGridSortCommandEvent Args) 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" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
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).
http://msdn.microsoft.com/msdnmag/is...g/default.aspx

HTH,
Suresh.

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

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.DataGridSortCommandEvent Args) 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

--
_____
DC G

Nov 18 '05 #3
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
"Suresh" <an*******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
I'm not sure why it still isn't working..

Try one more thing and see if this helps.
Change the following line

Dim dv As New DataView(DsAdvanced1.Tables(0))

to

Dim dv As New DataView(DsAdvanced1.Tables(0).DefaultView)

Suresh.

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

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.DataGridSortCommandEvent Args) 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" <an*******@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
> 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).
>>
http://msdn.microsoft.com/msdnmag/is...g/default.aspx >> HTH, > Suresh.
>> ----- DC Gringo wrote: -----
>> 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.DataGridSortCommandEvent Args) 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
>> --

> _____
> DC G
>>>

Nov 18 '05 #4
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;ui d=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.DataGridSortCommandEvent Args) Handles DataGrid1.SortComman
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;ui d=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
"Suresh" <an*******@discussions.microsoft.com> wrote in messag
news:C7**********************************@microsof t.com..
I'm not sure why it still isn't working.
Try one more thing and see if this helps Change the following lin
Dim dv As New DataView(DsAdvanced1.Tables(0)
t
Dim dv As New DataView(DsAdvanced1.Tables(0).DefaultView
Suresh
----- DC Gringo wrote: ----
Suresh...thank you for assisting. I added the & " ASC" as yo suggested Unfortunately, that did not change anything...the datagrid still no
sorting...it is posting and running the Sub as evidenced by m
lblWhatClicked.Text = "You clicked: " & e.SortExpression, but th table i still un-sorted
Could you take another look
Public Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e A System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handle
DataGrid1.SortComman
lblWhatClicked.Text = "You clicked: " & e.SortExpressio
Dim dv As New DataView(DsAdvanced1.Tables(0)

dv.Sort = e.SortExpression & " ASC
DataGrid1.DataSource = d
DataGrid1.DataBind(
End Su
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
"Suresh" <an*******@discussions.microsoft.com> wrote in messag

news:F3**********************************@microsof t.com..
Try changing the followin
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).
http://msdn.microsoft.com/msdnmag/is...g/default.aspx HTH, Suresh.
----- DC Gringo wrote: -----
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.DataGridSortCommandEvent Args) 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
--

_____
DC G

Nov 18 '05 #5
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").DefaultVie w
dv2.Sort = " clnName ASC"
'e.SortExpression &
DataGrid1.DataSource =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie w
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.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand

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

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

"Suresh" <an*******@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
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:
-------
<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.Load If Not Page.IsPostBack Then
Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;ui d=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.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;ui d=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 = e.SortExpression & " ASC"
DataGrid1.DataSource = ds.Tables("Books").DefaultView
DataGrid1.DataBind()
End Sub

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 type
'System.Data.DataView' cannot be converted to 'System.Data.DataTable'."
_____
DC G
"Suresh" <an*******@discussions.microsoft.com> wrote in message
news:C7**********************************@microsof t.com...
> I'm not sure why it still isn't working..
>> Try one more thing and see if this helps. > Change the following line
>> Dim dv As New DataView(DsAdvanced1.Tables(0))
>> to
>> Dim dv As New DataView(DsAdvanced1.Tables(0).DefaultView)
>> Suresh.
>> ----- DC Gringo wrote: -----
>> 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.DataGridSortCommandEvent Args) 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" <an*******@discussions.microsoft.com> wrote in
message > news:F3**********************************@microsof t.com...
>> 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).
>>>

http://msdn.microsoft.com/msdnmag/is...g/default.aspx >>> HTH,
>> Suresh.
>>> ----- DC Gringo wrote: -----
>>> 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.DataGridSortCommandEvent Args) 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
>>> --
>> _____
>> DC G
>>>>

Nov 18 '05 #6
> 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 followingdv2.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.DataGridSortCommandEvent Args) 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" <an*******@discussions.microsoft.com> wrote in messag
news:78**********************************@microsof t.com.. Sorry for that I made a mistake. I'm not familiar with VB.NET that much
S 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 an 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 A System.EventArgs) Handles MyBase.Loa
If Not Page.IsPostBack The
Dim objConn As SqlConnection = Ne SqlConnection("database=pubs;server=mysqlserver;ui d=myuname;password=mypwd" Dim objAdap As SqlDataAdapter = New SqlDataAdapter("selec 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.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand Dim objConn As SqlConnection = New SqlConnection("database=pubs;server=mysqlserver;ui d=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 = e.SortExpression & " ASC"

DataGrid1.DataSource = ds.Tables("Books").DefaultView
DataGrid1.DataBind()
End Sub
Hope this helps you narrow the problem.

Suresh.

Nov 18 '05 #7
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" <an*******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
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 w
Instead shoule be
DataGrid1.DataSource = dv

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


dv2.Sort = e.SortExpression & "DESC"
or
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 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").DefaultVie w
dv2.Sort = " clnName ASC"
'e.SortExpression &
DataGrid1.DataSource =
DsAdvanced1.Tables("sac1SelectSurvey1").DefaultVie w
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.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand

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

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

"Suresh" <an*******@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
> 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: > -------
><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.Load
> If Not Page.IsPostBack Then
> Dim objConn As SqlConnection = New

SqlConnection("database=pubs;server=mysqlserver;ui d=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.DataGridSortCommandEvent Args) Handles
DataGrid1.SortCommand
> Dim objConn As SqlConnection = New

SqlConnection("database=pubs;server=mysqlserver;ui d=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 = e.SortExpression & " ASC"

> DataGrid1.DataSource = ds.Tables("Books").DefaultView
> DataGrid1.DataBind()
> End Sub
>> Hope this helps you narrow the problem.

> Suresh.

Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ChrisM | last post by:
Can anyone please tell me what I'm doing wrong here. I have a Windows Form with a DataGrid on it, and I'm having real problems with the Sorting. It is easy to reproduce the problem I have. If...
4
by: Bill Todd | last post by:
I have a DataTable in a DataSet. A DataGrid is bound to the DataTable. When I assign a field to the DataTable.DefaultView.Sort property the order in the DataGrid does not change. What am I...
0
by: Robert Brinson | last post by:
Hello all! I'm running .NET Framework 1.1 using VS.NET 2003. I've got a mystery with a DataGrid. Below is the definition of the DataGrid from my aspx page: </asp:datagrid><asp:datagrid...
2
by: enak | last post by:
I can not get my datagrid to page. I have a datagrid that I can sort 2 of the columns. This works great. I added paging and when I display the dg it shows 5 pages. (I am showing page numbers at...
1
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current...
1
by: troyblakely | last post by:
I am having trouble sorting a datagrid. I have read numerous posts on this and other lists, and tried most of the suggestions, but none of them have worked for me yet. I populate a dataset from two...
5
by: DKC | last post by:
Hi, Using VB.NET. I have a datagrid having a strongly typed array of objects as its data source. The data from the array of objects is displayed by means of a table style, which is fine, but...
4
by: skOOb33 | last post by:
I successfully autosized the columns and rows on my Datagrid, and am now facing another issue. Having the sorting ability by clicking the column headers is key, but when I do that, it resizes all...
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.