|
-
Apr 29th, 2005, 01:30 PM
#1
Thread Starter
Lively Member
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:
For Each ColumnName In ColumnNames
objDC = New DataColumn(ColumnName, GetType(String))
objDT.Columns.Add(objDC)
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.
-
Apr 30th, 2005, 01:35 PM
#2
Re: Sorting a DataGrid
Use a dataview, sort on it, and bind the grid to the dataview.
-
May 3rd, 2005, 09:31 AM
#3
Thread Starter
Lively Member
Re: Sorting a DataGrid
Well, here is what I have for my sort:
VB Code:
Sub dg_sort(ByVal s As Object, ByVal e As DataGridSortCommandEventArgs)
objDV.Sort = e.SortExpression
dg.DataSource = objDV
dg.DataBind()
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?
-
May 3rd, 2005, 10:01 AM
#4
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|