Question : trouble with sorting columns on gridview in ASP.NET 2.0

I am having trouble sorting columns on  a  gridview in ASP.NET 2.0.  
I finally got paging to work, but the sorting does not seem to do anything (not sure how I re-populate the datatable in the code behind).
The events are firing just fine, but I can't seem to make it work (because I am binding the GV with a button click instead of at design time):

                                                                                            ForeColor="#333333" GridLines="None" HorizontalAlign="Left" PageSize="2" Width="600px" AllowPaging="True" AllowSorting="True" >
                                               
                                               
                                               
                                               
                                                   
                                                       
                                                       
                                                   

                                                                                                            Text="Name" DataNavigateUrlFormatString="editaffiliates.aspx?iden={0}" >
                                                       
                                                       
                                                   

                                                   
                                                       
                                                       
                                                   

                                                   
                                                       
                                                       
                                                   

                                                   
                                                       
                                                       
                                                   

                                                   
                                                       
                                                       
                                                   

                                               

                                               
                                               
                                               
                                               
                                               
                                           
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
Partial Class affiliatecomm
        Inherits System.Web.UI.Page
 
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'Response.CacheControl = "private"
            'Response.Expires = 0
            'Response.AddHeader("pragma", "no-cache")   
 
        End Sub
 
        Protected Sub btnGetAffCommData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetAffCommData.Click
            Me.PopulateAffSummData()
        End Sub
 
        Protected Sub PopulateAffSummData()
            Dim ds As DataSet = DB.GetDS("dbo.AffCommDueSummary", False)
 
            If ds.Tables(0).Rows.Count > 0 Then
                Me.gvCommSummary.DataSource = ds.Tables(0)
                Me.gvCommSummary.DataBind()
            End If
 
        End Sub
 
        Private Function ConvertSortDirectionToSql(ByVal sortDirection As SortDirection) As String
            Dim NewSortDirection As String = String.Empty
            Select Case sortDirection
                Case sortDirection.Ascending
                    NewSortDirection = "ASC"
                Case sortDirection.Descending
                    NewSortDirection = "DESC"
                Case Else
                    NewSortDirection = ""
            End Select
            Return NewSortDirection
        End Function
 
        Protected Sub gvCommSummary_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvCommSummary.PageIndexChanging
            Me.PopulateAffSummData()
 
            gvCommSummary.PageIndex = e.NewPageIndex
            gvCommSummary.DataBind()
        End Sub
 
        Protected Sub gvCommSummary_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvCommSummary.Sorting
            Me.PopulateAffSummData()
 
            Dim dt As DataTable = gvCommSummary.DataSource
 
            If Not dt Is Nothing Then
                Dim dataView As DataView = New DataView(dt)
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection)
 
                gvCommSummary.DataSource = dataView
                gvCommSummary.DataBind()
            End If
        End Sub
    End Class

Answer : trouble with sorting columns on gridview in ASP.NET 2.0

I went ahead and took the wizard approach and decided not to build a gridview from scratch :)    .
Once I figured out I could take advantage of the datasource control and load the connection string from the Page_Init (instead of at design-time), there was no point in building a gridView from scratch.  Also, the sort feature works fine if you use a datasource.
Random Solutions  
 
programming4us programming4us