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. Database & SysAdmin
  3. Database
  4. Question concerning Sorting DataGrids

Question concerning Sorting DataGrids

Scheduled Pinned Locked Moved Database
questiondatabasewinformsalgorithmshelp
3 Posts 2 Posters 1 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.
  • N Offline
    N Offline
    nad2263
    wrote on last edited by
    #1

    Hello, In Windows Forms, I have a datagrid that is sortable by clicking on the column headers. Say 2nd record is already selected and I sort the datagrid. Now this previously selected row is unselected. At this point how do I get the index to this previously selected row to have it selected again. The issue I'm referring is that after sorting the dataset still has the same indexing order on the datagrid, meaning, a particular row's index before or after sorting remains intact eventhough after sorting it's position on the datagrid view has changed. Thank you.

    D 2 Replies Last reply
    0
    • N nad2263

      Hello, In Windows Forms, I have a datagrid that is sortable by clicking on the column headers. Say 2nd record is already selected and I sort the datagrid. Now this previously selected row is unselected. At this point how do I get the index to this previously selected row to have it selected again. The issue I'm referring is that after sorting the dataset still has the same indexing order on the datagrid, meaning, a particular row's index before or after sorting remains intact eventhough after sorting it's position on the datagrid view has changed. Thank you.

      D Offline
      D Offline
      Dominic Farr
      wrote on last edited by
      #2

      I'm trying to do the same thing. If you find out a way do it let me know, Thank you

      1 Reply Last reply
      0
      • N nad2263

        Hello, In Windows Forms, I have a datagrid that is sortable by clicking on the column headers. Say 2nd record is already selected and I sort the datagrid. Now this previously selected row is unselected. At this point how do I get the index to this previously selected row to have it selected again. The issue I'm referring is that after sorting the dataset still has the same indexing order on the datagrid, meaning, a particular row's index before or after sorting remains intact eventhough after sorting it's position on the datagrid view has changed. Thank you.

        D Offline
        D Offline
        Dominic Farr
        wrote on last edited by
        #3

        Two part to this. Part1 (Save Row ID) Each time a user selects a row in your DataGrid you need to save the row's unique idenifier. Part2 (Re select Row based on ID) After sorting, locate the new location of your selected row using the saved unique row identifier. Example Part 1

        // set selected row's id in view state
        IEnumerator enu = ((DataView)DataGrid1.DataSource).GetEnumerator();
        int index = 0;
        while( index <= DataGrid1.SelectedIndex )
        {
        enu.MoveNext();
        index++;
        }

        DataRowView rowView = enu.Current as DataRowView;
        DataRow row = rowView.Row;
        ViewState["item"] = (int)row["ID"];

        Example Part 2

        DataView sortView = dataSet1.MyTable.DefaultView;

        // numberDiv is a static int
        if( ( numberDiv % 2 ) == 0 )
        {
        sortView.Sort = e.SortExpression + " ASC";
        }
        else
        {
        sortView.Sort = e.SortExpression + " DESC";
        }
        numberDiv++;

        // set new sort into ViewState
        ViewState["sort"] = sortView.Sort;

        // set sorted dataview
        DataGrid1.DataSource = sortView;

        // bind data
        DataGrid1.DataBind();

        // here reselected your row
        // check that you have an item selected
        if( ViewState["item"] != null )
        {
        int id = (int)ViewState["item"];
        int index = -1;
        IEnumerator enu = sortView.GetEnumerator();
        while(enu.MoveNext())
        {
        index++;
        DataRowView rowView = enu.Current as DataRowView;
        DataRow row = rowView.Row;
        int rowID = (int)row["ID"];
        if(rowID == id)
        {
        break;
        }
        }
        DataGrid1.SelectedIndex = index;
        }

        Hope this helps. My thanks to minhpc_bk

        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