Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView DataSource has no Rows When Sorting 1

Status
Not open for further replies.

acent

Technical User
Feb 17, 2006
247
0
0
US
I have a GridView with the datasource equal to a datatable. Whne I click on the header row to sort the gridview, the gridview.datasource as no rows. Why?

My code is:
Code:
  Private Sub gvNSFs_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
    Dim dt As New DataTable(gvNSFs.DataSource)
    Dim dv As New DataView(dt)
    lblError.Text = dt.Rows.Count 'returns 0
    'dv.Sort = e.SortExpression & " " & convertSortDirection(e.SortDirection)
    gvNSFs.DataSource = dv
    gvNSFs.DataBind
  End Sub
  
  Private Function convertSortDirection(sd As SortDirection)
    Dim strSD As String = ""
    Select (sd)
    Case SortDirection.Ascending
      strSD = "ASC"
    Case SortDirection.Descending
      strSD = "DESC"
    End Select
    convertSortDirection = strSD
  End Function

The gridview is populated with 2200+ rows.

Any help is appreciated.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
You either have to hit the db again to get the data,or store the dt in session or viewstate. You should consider not showing so much data at one time. Use paging.
 
Thanks jbenson001. I assume it is preferable to use the viewstate or session as opposed to hitting the db again. The data doesn't change that much.

Paging is my next mountain to climb.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
If the data does not change much, then I would suggest using caching. It is easy to implement and you can set a date/time for it to expire, this way you get any changes after a certain amount of time.

Also, when you do paging it will be the same thing, you will have to get the data again, and then bind the grid. I strongly suggest you use caching.
 
Caching - that's a fun topic... Can I manually force a recache?

Also, back to the original question about sorting... I got the thing sorted and all is well... except that the fields are always sorted as a string. In other words, I have an amount field that that is putting 499.00 in front of 50.00. Is there a way to force a field to be sorted as an number rather than a string?

Tanks for the help.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
You can force the cache to reload.

Is your amount field defined as a "numeric" data type.

I have never run into this problem before.
 
No, but come to think of it, it isn't really a field anyway... The data is just barfed out to the user
Code:
          <asp:TemplateField
            SortExpression="amt"
            HeaderText="Amount">
            <itemTemplate>
              <%#DataBinder.Eval(Container.DataItem,"amt") %>
            </itemTemplate>
          </asp:TemplateField>
I haven't seen a datatype setting for the itemtemplate, so how do I change this to set a datatype?

Thanks for your help!

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
NO, what I mean is, what is the datatype of the amount field in your database?
 
Oh. Double format (access db).

I know these are pretty cheese questions and I really do appreciate the help.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
Alright... now I feel like an idiot. In my SQL, I used the access FORMAT() which converts it to a string.

By removing that, my grid now sorts as it should. I'll just have to do some more fancy footwork to get it to display 2 decimals to display the amounts as the boss wants.


"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
It shouldn't be too hard to do. You can do it on the HTML side with a string.format() or you can do it on the server side in the RowDataBound event, using a findcontrol() and the string.format() method as well. Post back if you need help.
 
Is there a way to force a field to be sorted as an number rather than a string?

In your query you need to tell it to pull the values as a number.

In Oracle it is:
Code:
To_Number(ID)




Thanks,
Chilly442
---------------------------------------
If I lived anywhere else I'd be Sunny442
 
Thnaks guys. I did play with the String.Format() and got it to look and work the way we want it to.

"If it's stupid but works, it isn't stupid."
-Murphy's Military Laws
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top