Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sorting DataGrid when datasource is a DataSet

Sorting DataGrid when datasource is a DataSet

Scheduled Pinned Locked Moved C#
questionalgorithms
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris_Green
    wrote on last edited by
    #1

    I have a MyDataSet : DataSet class which contains 3 talbes and 2 data relations. This is set to a custom datagrid which can expand to show something like Users -> tickets for user -> details of ticket. I'm sure this is entirely simple, but how do I sort while keeping my dataset as the datasource? When I google they always discard the dataset and do something like: MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = dv; _dgUsers.DataBind(); This is useless as I've just lost my dataset. I tried modifying the view of the table inside the dataset but it just gets ignored (although it remains set when I sort again): MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = **ds**; _dgUsers.DataBind(); How should it be done? When I clone the DataSet it seems to work. Why is that?

    K 1 Reply Last reply
    0
    • C Chris_Green

      I have a MyDataSet : DataSet class which contains 3 talbes and 2 data relations. This is set to a custom datagrid which can expand to show something like Users -> tickets for user -> details of ticket. I'm sure this is entirely simple, but how do I sort while keeping my dataset as the datasource? When I google they always discard the dataset and do something like: MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = dv; _dgUsers.DataBind(); This is useless as I've just lost my dataset. I tried modifying the view of the table inside the dataset but it just gets ignored (although it remains set when I sort again): MyDataSet ds = (MyDataSet) _dgUsers.DataSource; DataView dv = ds.Tables[0].DefaultView; dv.Sort = "something ASC"; _dgUsers.DataSource = **ds**; _dgUsers.DataBind(); How should it be done? When I clone the DataSet it seems to work. Why is that?

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      Perhaps you should try: ds.Tables[0].DefaultView.Sort = "something ASC"; _dgUsers.DataSource = ds; _dgUsers.DataBind(); Ben

      C 1 Reply Last reply
      0
      • K kubben

        Perhaps you should try: ds.Tables[0].DefaultView.Sort = "something ASC"; _dgUsers.DataSource = ds; _dgUsers.DataBind(); Ben

        C Offline
        C Offline
        Chris_Green
        wrote on last edited by
        #3

        That's what I did in the second code block. Doesn't matter if you touch the dg.DataSource or set it to a local variable as its a pointer to the same dataset object. Like I said, I would see "something ASC" set the next time I would sort. The code was there to toggle so clicking it you could see it going from ASC to DESC and back again. However, it did nothing. For some reason we needed to clone the dataset and set it back to the datagrid.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups