Results 1 to 4 of 4

Thread: Sorting a DataGrid [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    Resolved Sorting a DataGrid [RESOLVED]

    I'm new to ASP.NET, so this might be a very silly question.

    We have an application that spits out a text file in a tabular format. I'm working on an app that will grab that text file and display it in a datagrid that can be sorted. I got the reading in of the text file and the displaying in a datagrid done with no problems. I can't figure out how to sort it though.

    I can easily find how to do the sorting with Bound Columns - setting the 'SortExpression' property in the BoundColumn tag. But since I can't be positive on how many columns will be in the text file, I need to add the columns programmatically.

    VB Code:
    1. For Each ColumnName In ColumnNames
    2.      objDC = New DataColumn(ColumnName, GetType(String))
    3.      objDT.Columns.Add(objDC)
    4. Next

    But I can't find any way of setting the 'SortExpression' property this way, so when ever I click on a column header, it always errors out saying that it can't find the column name.

    How do I do this?
    Last edited by shadowfyre; May 3rd, 2005 at 10:02 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Sorting a DataGrid

    Use a dataview, sort on it, and bind the grid to the dataview.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    Re: Sorting a DataGrid

    Well, here is what I have for my sort:

    VB Code:
    1. Sub dg_sort(ByVal s As Object, ByVal e As DataGridSortCommandEventArgs)
    2.     objDV.Sort = e.SortExpression
    3.     dg.DataSource = objDV
    4.     dg.DataBind()
    5. End Sub

    Where dg is my datagrid and objDV is my DataView.

    When I click on the column header "Cost", I get a "System.Index.OutOfRangeException: Cannot find column Cost" and it highlights the 'objDV.Sort = e.SortExpression' in the above sort subroutine. Is there someway to name the columns programatically like with BoundColumns?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    Re: Sorting a DataGrid

    As it turns out, the prediction I made in my first post is correct. It was a silly question. As I was reading the column names in from the text file, a space was getting added to the beginning.

    For instance, the headers looked like this:
    Code:
    +------+------+-----+
    |      | Cost |     |
    |      | Per  |     |
    | Name | Hour | Age |
    +------+------+-----+
    So 'Name' and 'Age' ended up having spaces in front of the column name and 'Cost Per Hour' didn't. All fixed now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width