Advertisement

06.05.2008 at 08:41PM PDT, ID: 23462633
[x]
Attachment Details

How to sort a manual GridView?

Asked by riceman0 in Programming for ASP.NET, Microsoft Visual Basic.Net

So I believe I finally figured out how to populate a gridview myself; by using a datatable and manually populating it, then binding the GridView to it.  I wrote a generic little function below that does it.  (Ignore my custom wrapper class cMyDataTable, it doesn't do much.)  The problem is, when I load my GridView this way, and set the "GridView.AllowSorting = True" property, it doesn't work.  The column header becomes a link, but when I click on it I get the error:

"The GridView 'GridView9' fired event Sorting which wasn't handled."

Really?  It doesn;t have a default way to sort data?  I've looked at the help and it is very perplexing and doesn't seem to apply to my odd way of loading data.  Does anybody have any guidance (and preferably some sample code) on how to make my grid sortable?  Thanks very much!Start Free Trial
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:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim pdb As cPlansDatabase = Session("Database")
        Me.Label3.Text = Trim(Str(pdb.PlansTable.RowCount))
        GridView9.AllowSorting = True
        FillGridTable(pdb.PlansTable, GridView9, New String() {"Plan Number", "Project Number"}, New String() {"Plan #", "Project #"})
 
    End Sub
 
 
 
    Private Sub FillGridTable(ByVal mt As cMyDataTable, ByVal gv As GridView, ByVal fields() As String, Optional ByVal colnames() As String = Nothing)
 
        Dim dt As New Data.DataTable
 
        If Not colnames Is Nothing Then
            For Each s As String In colnames
                dt.Columns.Add(s)
            Next
        Else
            For Each s As String In fields
                dt.Columns.Add(s)
            Next
        End If
 
        Dim dr As Data.DataRow
 
        Dim o(fields.Length - 1) As Object
        Dim i As Integer
 
        For n As Integer = 0 To mt.RowCount - 1
            dr = mt.Table.Rows(n)
 
            i = 0
 
            For m As Integer = 0 To fields.Length - 1
                o(i) = dr.Item(fields(m))
                i += 1
            Next
 
            dt.Rows.Add(o)
        Next
 
        gv.DataSource = dt
        gv.DataBind()
 
    End Sub
[+][-]06.05.2008 at 08:56PM PDT, ID: 21726097

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 08:58PM PDT, ID: 21726107

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 09:13PM PDT, ID: 21726168

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 09:19PM PDT, ID: 21726187

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 10:02PM PDT, ID: 21726326

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 10:21PM PDT, ID: 21726409

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, Microsoft Visual Basic.Net
Sign Up Now!
Solution Provided By: funwithdotnet
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628