CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums




Can Reply:  No Members Can Edit: No Online: Yes
Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.faq_frequently_asked_questions Tags:
Item Type: NewsGroup Date Entered: 1/24/2006 6:55:57 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
NR
XPoints: N/A Replies: 185 Views: 237 Favorited: 0 Favorite
186 Items, 10 Pages 1 2 3 4 5 6 7 8 9 10 |< << Go >> >|
LiamHughes1981
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/6/2006 8:55:49 AM

0

Fixed my problem. Don't USE THE CODE IN THIS THREAD if your intend to have sorting and paging on at the same time! Use a ObjectDataSource mapping to a buisiness tier object that returns your DataTable.

 Object Data Source does all the Sorting and paging for you. This code will make your sorting and paging work incorrectly, don't believe me try sorting your data then click onto a different page, you'll see what I mean.

StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/6/2006 11:16:39 AM

0

LiamHughes1981:

Fixed my problem. Don't USE THE CODE IN THIS THREAD if your intend to have sorting and paging on at the same time! Use a ObjectDataSource mapping to a buisiness tier object that returns your DataTable.

 Object Data Source does all the Sorting and paging for you. This code will make your sorting and paging work incorrectly, don't believe me try sorting your data then click onto a different page, you'll see what I mean.

Actually, this code does handle paging and sorting quite well. Don't know what you did wrong, but I know it works cause I created and tested the code myself. The ObjectDataSource is one solution, but the entire point of this thread is to provide a solution to not using it.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
maniac
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/7/2006 3:31:08 PM

0

Ryan:

  • Thank you for posting.
    • I was in several cities yesterday.
      • Sorry for not responding immediately.
    • I had printed out the entire thread to read this weekend, so halfway through that ... LOL
    • The light bulb went off when I reread the post this morning.
      • I might have grabbed the header name instead of the SQLSVR column name.
        • That would explain the error message.
      • I'll finish reading the thread this weekend & try again Tuesday when code available.
      • Have a good weekend!

: )

 


A+ BS MCSE OOP Security+ Web Developer
Get creative, not even.
soporteweb
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/10/2006 6:33:38 PM

0

Ryan,

Your code run perfectly but I still having some troubles if i set the gridview datakeynames with the field's name; and i need it to have access to the rest of the information about that record. This is the error: Item has already been added. Key in dictionary: 'idcontacto'  Key being added: 'idcontacto'

Any ideas?

SoporteWeb.-

AdamDuritz99
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/12/2006 7:46:00 AM

0

soporteweb:

Ryan,

Your code run perfectly but I still having some troubles if i set the gridview datakeynames with the field's name; and i need it to have access to the rest of the information about that record. This is the error: Item has already been added. Key in dictionary: 'idcontacto'  Key being added: 'idcontacto'

Any ideas?

SoporteWeb.-

 

Yeah, I have the same exact problem. 

amitbatra.mca
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/12/2006 1:39:59 PM

0

Hi i tried ur code but its giving error
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/12/2006 1:48:34 PM

0

amitbatra.mca:
Hi i tried ur code but its giving error

I can't help you without knowing the error.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/12/2006 1:50:34 PM

0

soporteweb:
Your code run perfectly but I still having some troubles if i set the gridview datakeynames with the field's name; and i need it to have access to the rest of the information about that record. This is the error: Item has already been added. Key in dictionary: 'idcontacto'  Key being added: 'idcontacto'

AdamDuritz99:
Yeah, I have the same exact problem.

I'll need to look into this. 


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
linch12
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/13/2006 9:12:15 PM

0

I have the same problem when trying to run the code in code behind. Can't figure out why. here is my code.  Am I missing something? Thanks

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

PopulateItemList()

End Sub

Protected Sub gv_list_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv_list.PageIndexChanging

gv_list.DataSource = SortDataTable(gv_list.DataSource, True)

gv_list.PageIndex = e.NewPageIndex

Page.DataBind() 'need this line, or have to click twice on the page number 

pn_detail.Visible = False

End Sub

Protected Sub gv_list_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv_list.Sorting

GridViewSortExpression = e.SortExpression

Dim pageIndex As Integer = gv_list.PageIndex

gv_list.DataSource = SortDataTable(gv_list.DataSource, False) ' This is the line where the message pointed to."unable to cast from dataview to datatable"

gv_list.DataBind()

gv_list.PageIndex = pageIndex

end sub

Protected Sub PopulateItemList()

Dim dt As New DataTable

conn = functions.GetConnection()

If conn.State = ConnectionState.Closed Then

conn.Open()

End If

dt = SPROC.procGetItemListWithCurrentPrice(conn)

If dt.Rows.Count > 0 Then

Me.gv_list.DataSource = dt

Page.DataBind()

End If

If conn.State = ConnectionState.Open Then

conn.Close()

End If

Private Property GridViewSortDirection() As String

Get

Return IIf(ViewState("SortDirection") = Nothing, "DESC", ViewState("SortDirection"))

End Get

Set(ByVal value As String)

ViewState("SortDirection") = value

End Set

End Property

Private Property GridViewSortBLOCKED EXPRESSION As String

Get

Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))

End Get

Set(ByVal value As String)

ViewState("SortExpression") = value

End Set

End Property

Private Function GetSortDirection() As String

Select Case GridViewSortDirection

Case "ASC"

GridViewSortDirection = "DESC"

Case "DESC"

GridViewSortDirection = "ASC"

End Select

Return GridViewSortDirection

End Function

Protected Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView

If Not dataTable Is Nothing Then

Dim dataView As New DataView(dataTable)

If GridViewSortExpression <> String.Empty Then

If isPageIndexChanging Then

dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)

Else

dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())

End If

End If

Return dataView

Else

Return New DataView()

End If

End Function
 
StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/13/2006 11:44:53 PM

0

linch12:
I have the same problem when trying to run the code in code behind. Can't figure out why. here is my code.  Am I missing something?

What is the error message that you're getting? 


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
linch12
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/16/2006 8:54:29 PM

0

 "Cannot cast system.data.dataview to system.data.datatable".

I have also quoted the message at the line where it referred to in code (highlighted in red).

 Thanks

StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/16/2006 9:22:39 PM

0

linch12:
 "Cannot cast system.data.dataview to system.data.datatable". I have also quoted the message at the line where it referred to in code (highlighted in red).
 

Missed that line. It looks like the datasource of the GridView that you're passing to the function is a DataView and not a DataTable, hence the exception you're getting.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
linch12
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/16/2006 9:41:50 PM

0

The datasource for the gridview is datatable. following is the code

Dim dt As New DataTable

conn = functions.GetConnection()

If conn.State = ConnectionState.Closed Then

conn.Open()

End If

dt = SPROC.procGetItemListWithCurrentPrice(conn)

If dt.Rows.Count > 0 Then

Me.gv_list.DataSource = dt

Page.DataBind()

End If

If conn.State = ConnectionState.Open Then

conn.Close()

End If

paulafernandes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/23/2006 12:53:11 AM

0

Hello!

I'm using the VB version of the code, and I have 2 questions:

1) On the Private Property GridViewSortDirection I had to replace the line

Return IIf(ViewState("SortDirection") = Nothing, "DESC", ViewState("SortDirection")) with Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection")), because the sorting only worked on the second time. Why does this happens?

2) I have to use this code on 8 different forms, so I was thinking to put it on a class, but I don't know how to do this. I've read about this on a pages back, but I didn't understand it.

Can anyone give me some hints on this matter?

 
Thank's a lot!

Paula
 

StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/23/2006 4:00:33 AM

0

paulafernandes:
1) On the Private Property GridViewSortDirection I had to replace the line

Return IIf(ViewState("SortDirection") = Nothing, "DESC", ViewState("SortDirection")) with Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection")), because the sorting only worked on the second time. Why does this happens?

Looks like you've found a bug. I've gone ahead and updated the examples.

paulafernandes:
2) I have to use this code on 8 different forms, so I was thinking to put it on a class, but I don't know how to do this. I've read about this on a pages back, but I didn't understand it.

All you have to do is put them in a class and make them public. Then just call the methods from that class.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
paulafernandes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/23/2006 10:02:35 AM

0

StrongTypes:
All you have to do is put them in a class and make them public. Then just call the methods from that class.

Can you tell me some articles or sites where I can learn how to do this?

Thank's a lot

Paula 

StrongTypes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/23/2006 11:44:09 AM

0

paulafernandes:
Can you tell me some articles or sites where I can learn how to do this?

It's pretty simple. For example:

Public Class MyClass1
     Public Shared Function Count() As Integer
        Return 2
    End Function
End Class

Public Class MyClass2
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
       Response.Write(MyClass1.Count())
    End Sub
End Class

Let me know if that helps.


Ryan Olshan
Microsoft MVP, ASP.NET
Blog | Group | Website | Strong Coders Community

How to ask a question
paulafernandes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/23/2006 5:25:00 PM

0

I think I'm almost there... I hope!

What I did:

1) On the aspx.vb page:

 

    Protected Sub gvSupplier_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvSupplier.Sorting
Dim PagSort As PagingSorting = New PagingSorting

PagSort.GridViewSortExpression = e.SortExpression
Dim pageIndex As Integer = Me.gvSupplier.PageIndex
Me.gvSupplier.DataSource = PagSort.SortDataTable(Me.gvSupplier.DataSource, False)
Me.gvSupplier.DataBind()
Me.gvSupplier.PageIndex = pageIndex
End Sub

Protected Sub
gvSupplier_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvSupplier.PageIndexChanging
Dim PagSort As PagingSorting = New PagingSorting

Me.gvSupplier.DataSource = PagSort.SortDataTable(Me.gvSupplier.DataSource, True)
Me.gvSupplier.PageIndex = e.NewPageIndex
Me.gvSupplier.DataBind()
End Sub
  2) I created an class page called PagingSorting.vb and add:
 
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class PagingSorting
    Inherits System.Web.UI.Page

    Public Property GridViewSortDirection() As String
        Get
            Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
        End Get
        Set(ByVal value As String)
            ViewState("SortDirection") = value
        End Set
    End Property

    Public Property GridViewSortBLOCKED EXPRESSION As String
        Get
            Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
        End Get
        Set(ByVal value As String)
            ViewState("SortExpression") = value
        End Set
    End Property

    Public Function GetSortDirection() As String
        Select Case GridViewSortDirection
            Case "ASC"
                GridViewSortDirection = "DESC"
            Case "DESC"
                GridViewSortDirection = "ASC"
        End Select
        Return GridViewSortDirection
    End Function

    Public Function SortDataTable(ByVal dataTable As DataTable, ByVal isPageIndexChanging As Boolean) As DataView
        If Not dataTable Is Nothing Then
            Dim dataView As New DataView(dataTable)
            If GridViewSortExpression <> String.Empty Then
                If isPageIndexChanging Then
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
                Else
                    dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
                End If
            End If
            Return dataView
        Else
            Return New DataView()
        End If
    End Function

End Class
  

 The paging works fine, but the sorting doesn't. It only works on the first time. I did some debugging and realized that the PageIndex is always = 0.

How can I retrieve the right PageIndex?

Thank's a lot once again!

Paula 

dotcarlisle
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/23/2006 10:00:26 PM

0

I had to add this line:

m_SortCount += 1

to the SortDataTable function (VB.NET) and then check it in the sorting event ... for some reason my sorting event is fired twice.  I saw somebody had the same issue so I thought I'd see if anyone knows why this event is fired twice? 

Hope somebody can solve this mystery.

Jon


StrollAway
StrollAway.com
[email protected]
paulafernandes
Asp.Net User
Re: HOW TO: Using sorting / paging on GridView w/o a DataSourceControl DataSource10/24/2006 2:12:59 PM

0

I FOUND IT!!!!

I got it working!!!! Yeeeeessssss......!!!!

I replaced the "viewstate" variable by a session variable! It was so simple!!!!

Thank's all!!!Yes

Paula
 

186 Items, 10 Pages 1 2 3 4 5 6 7 8 9 10 |< << Go >> >|


Free Download:




   
  Privacy | Contact Us
All Times Are GMT