Sort DataGrid that is half & half

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
I have a data grid that has two button columns in it that are created at design time. Then I create a dataview with 5 more columns programmaticaly during runtime and bind these to the grid. The result is a 7 column datagrid.

The trouble is when I try to sort. When a user clicks on the column heading my sort command is fired but I cannot determine which column was clicked on. This makes it impossible to do the sort.

Any suggestions?
 
The plot thickens....but doens't get resolved.
I am thinking about this more and here is what I have.

Here is some code I have to create the datagrid and bind it.
The only thing I did during design time is drop a data grid on the form, nothing else.


Code:
Dim strOrderBy As String
    Dim dv As New DataView
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Try
                strOrderBy = "ID ASC"
                ViewState("strOrderBy") = strOrderBy
                ViewState("Column") = "ID"
                ViewState("Order") = "ASC"


                Dim ds2 As New DataSet
                'DataGrid2.AutoGenerateColumns = False
                DataGrid2.AllowSorting = True
                DataGrid2.AllowPaging = True


                'Get a DataSet object filled with data
                ds2 = Makeds()

                'Create ID column & add to DataGrid
                Dim col As New BoundColumn
                col.HeaderText = "User ID"
                col.DataField = "ID"
                col.SortExpression = "ID"
                DataGrid2.Columns.Add(col)

                'Create Name column & add to DataGrid
                col = New BoundColumn
                col.HeaderText = "User Name"
                col.DataField = "Name"
                col.SortExpression = "Name"
                DataGrid2.Columns.Add(col)

                'Create Address column & add to DataGrid
                col = New BoundColumn
                col.HeaderText = "User Address"
                col.DataField = "Address"
                col.SortExpression = "Address"
                DataGrid2.Columns.Add(col)

                'DataGrid data binding
                DataGrid2.DataSource = ds2.Tables(0)
                DataGrid2.DataBind()
            Catch ex As Exception
            End Try
        End If
        dtgCustBind()
    End Sub
    Function Makeds() As DataSet
        Dim myDataTable As DataTable = New DataTable("ParentTable")
        Dim myDataColumn As DataColumn
        Dim myDataRow As DataRow
        Dim myDataset As DataSet
        Dim j As Integer
        Try
            ' Create new DataColumn, set DataType, ColumnName and add to DataTable.    
            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "ID"
            myDataColumn.ReadOnly = True
            '  Add the Column to the DataColumnCollection.
            myDataTable.Columns.Add(myDataColumn)

            '  Create second column.
            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "Name"
            myDataColumn.AutoIncrement = False
            myDataColumn.ReadOnly = False

            ' Add the column to the table.
            myDataTable.Columns.Add(myDataColumn)

            '  Create third column.
            myDataColumn = New DataColumn
            myDataColumn.DataType = System.Type.GetType("System.String")
            myDataColumn.ColumnName = "Address"
            myDataColumn.AutoIncrement = False
            myDataColumn.ReadOnly = False

            ' Add the column to the table.
            myDataTable.Columns.Add(myDataColumn)

            ' Instantiate the DataSet variable.
            myDataset = New DataSet
            ' Add the new DataTable to the DataSet.
            myDataset.Tables.Add(myDataTable)

            For j = 1 To 12
                myDataRow = myDataTable.NewRow()
                myDataRow("id") = j.ToString
                myDataRow("Name") = (j + 100).ToString
                myDataRow("Address") = j.ToString & " Street"
                myDataTable.Rows.Add(myDataRow)
            Next
            Return myDataset
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Function

    Private Sub DataGrid2_SortCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid2.SortCommand

        If ViewState("Order") = "ASC" Then
            strOrderBy = e.SortExpression & " DESC"
            ViewState("Order") = "DESC"
        Else
            strOrderBy = e.SortExpression & " ASC"
            ViewState("Order") = "ASC"
        End If

        ViewState("strOrderBy") = strOrderBy
        ViewState("Column") = e.SortExpression()
        dtgCustBind()

    End Sub

    Private Sub DataGrid2_PageIndexChanged1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid2.PageIndexChanged

        Me.DataGrid2.CurrentPageIndex = e.NewPageIndex
        dv.Sort = ViewState("Column") & " " & ViewState("Order")
        Me.DataGrid2.DataSource = dv
        Me.DataGrid2.DataBind()

    End Sub

    Public Sub dtgCustBind()

        Dim ds2 As DataSet
        ds2 = Makeds()
        
        Dim dtbl As DataTable = ds2.Tables(0)
        dv = New DataView(dtbl)
        dv.Sort = ViewState("strOrderBy")
        Me.DataGrid2.DataSource = dv
        Me.DataGrid2.DataBind()

    End Sub


End Class


So out of all this code the one mighty line is this one:

'DataGrid2.AutoGenerateColumns = False

If this line is commented I get a datagrid that initially has twice the columns it should have but after a click on a sort column it looks as normal and it sorts. Very nice, except for the first look double columns.

If I uncomment that line I get around the double columns problem and I have a nice looking datagrid on load. If I click on a column to sort the whole thing vanishes!!!

What is going on?
 
Back
Top