Very odd Sort problem when modifying DGV bound data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sklett

    Very odd Sort problem when modifying DGV bound data

    (I posted this in a databinding NG, but it's a VERY low traffic NG so I
    thought I would post here as well. I hope no one minds too much, if you do
    I'm sorry)

    I have a DGV that is bound to a DataTable that is loaded during application
    startup. I can sort with all the columns and everything works fine. I have
    a method that can be invoked from a context menu that will loop through the
    rows of the bound table and modify a specific cell in each row. After
    modifying the rows sorting doesn't work for that column anymore.

    To give a more familiar example:
    Load the Products table from the Northwind DB
    Bind to a DGV via a BindingSource object
    Test teh sorting by clicking on the DGV columns
    add a button and in the click handler, loop through the DataRow collection
    and set the ProductID column to the value using an int that increments in
    the loop.
    Try and sort the ProductID column.

    You will get incorrect results, it will sort 20 or so rows, then jump to
    another number, then jump again.. it's broken.

    I've tried everything I can think of:
    calling EndEdit on each row
    reseting bindings
    etc

    Nothing seems to solve it. I'm not one to cry "bug" but this sure seems
    like one to me.

    Anyone ever experience this? Anyone happen to have a demo application setup
    with Northwind they could quickly check this? I've spent over a week now
    trying to find the problem and I can't. :(

    Any help or input greatly appreciated!

    Thanks for reading,
    Steve



  • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

    #2
    RE: Very odd Sort problem when modifying DGV bound data

    Have you tried re-binding the DGV after you have revised the datasource?
    Also, Datatable has a DefaultView property which sports a Sort property that
    you can set, and then bind to the View instead of the table.
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "sklett" wrote:
    (I posted this in a databinding NG, but it's a VERY low traffic NG so I
    thought I would post here as well. I hope no one minds too much, if you do
    I'm sorry)
    >
    I have a DGV that is bound to a DataTable that is loaded during application
    startup. I can sort with all the columns and everything works fine. I have
    a method that can be invoked from a context menu that will loop through the
    rows of the bound table and modify a specific cell in each row. After
    modifying the rows sorting doesn't work for that column anymore.
    >
    To give a more familiar example:
    Load the Products table from the Northwind DB
    Bind to a DGV via a BindingSource object
    Test teh sorting by clicking on the DGV columns
    add a button and in the click handler, loop through the DataRow collection
    and set the ProductID column to the value using an int that increments in
    the loop.
    Try and sort the ProductID column.
    >
    You will get incorrect results, it will sort 20 or so rows, then jump to
    another number, then jump again.. it's broken.
    >
    I've tried everything I can think of:
    calling EndEdit on each row
    reseting bindings
    etc
    >
    Nothing seems to solve it. I'm not one to cry "bug" but this sure seems
    like one to me.
    >
    Anyone ever experience this? Anyone happen to have a demo application setup
    with Northwind they could quickly check this? I've spent over a week now
    trying to find the problem and I can't. :(
    >
    Any help or input greatly appreciated!
    >
    Thanks for reading,
    Steve
    >
    >
    >
    >

    Comment

    • sklett

      #3
      Re: Very odd Sort problem when modifying DGV bound data

      Thank you for the reply Peter,

      Yes, my mistake, I am binding to the DataTable.Defau ltView and also
      specifying the Sort string via the DataTable.Defau ltView.Sort property.

      I'm checked the the DefaultView is sorting correctly and it is, if I print
      out the values to the console everything is correct, but once the DGV
      displays the items it makes a mess of it all.
      very strange.


      "Peter Bromberg [C# MVP]" <pbromberg@yaho o.yabbadabbadoo .comwrote in
      message news:CAC49CBE-2C1C-4B24-807E-97962EDBBC10@mi crosoft.com...
      Have you tried re-binding the DGV after you have revised the datasource?
      Also, Datatable has a DefaultView property which sports a Sort property
      that
      you can set, and then bind to the View instead of the table.
      Peter
      >
      --
      Site: http://www.eggheadcafe.com
      UnBlog: http://petesbloggerama.blogspot.com
      Short urls & more: http://ittyurl.net
      >
      >
      >
      >
      "sklett" wrote:
      >
      >(I posted this in a databinding NG, but it's a VERY low traffic NG so I
      >thought I would post here as well. I hope no one minds too much, if you
      >do
      >I'm sorry)
      >>
      >I have a DGV that is bound to a DataTable that is loaded during
      >application
      >startup. I can sort with all the columns and everything works fine. I
      >have
      >a method that can be invoked from a context menu that will loop through
      >the
      >rows of the bound table and modify a specific cell in each row. After
      >modifying the rows sorting doesn't work for that column anymore.
      >>
      >To give a more familiar example:
      >Load the Products table from the Northwind DB
      >Bind to a DGV via a BindingSource object
      >Test teh sorting by clicking on the DGV columns
      >add a button and in the click handler, loop through the DataRow
      >collection
      >and set the ProductID column to the value using an int that increments in
      >the loop.
      >Try and sort the ProductID column.
      >>
      >You will get incorrect results, it will sort 20 or so rows, then jump to
      >another number, then jump again.. it's broken.
      >>
      >I've tried everything I can think of:
      >calling EndEdit on each row
      >reseting bindings
      >etc
      >>
      >Nothing seems to solve it. I'm not one to cry "bug" but this sure seems
      >like one to me.
      >>
      >Anyone ever experience this? Anyone happen to have a demo application
      >setup
      >with Northwind they could quickly check this? I've spent over a week now
      >trying to find the problem and I can't. :(
      >>
      >Any help or input greatly appreciated!
      >>
      >Thanks for reading,
      >Steve
      >>
      >>
      >>
      >>

      Comment

      • RobinS

        #4
        Re: Very odd Sort problem when modifying DGV bound data

        If you are using .Net 2.0, you might try putting a BindingSource between
        your data source and your DGV, and using that to sort.

        BindingSource myBindingSource = new BindingSource() ;
        myBindingSource .DataSource = myDataTable;
        myDataGridView. DataSource = myBindingSource ;

        Any changes you make to the underlying data source are immediately
        displayed to the bound control via the BindingSource. It's kind like
        lubrication in a bunch of gears. And you can do sorts and filters and all
        kinds of things with the BindingSource.

        Robin S.
        --------------------------------------
        "sklett" <s@s.comwrote in message
        news:ObHA2NabHH A.4520@TK2MSFTN GP06.phx.gbl...
        Thank you for the reply Peter,
        >
        Yes, my mistake, I am binding to the DataTable.Defau ltView and also
        specifying the Sort string via the DataTable.Defau ltView.Sort property.
        >
        I'm checked the the DefaultView is sorting correctly and it is, if I
        print out the values to the console everything is correct, but once the
        DGV displays the items it makes a mess of it all.
        very strange.
        >
        >
        "Peter Bromberg [C# MVP]" <pbromberg@yaho o.yabbadabbadoo .comwrote in
        message news:CAC49CBE-2C1C-4B24-807E-97962EDBBC10@mi crosoft.com...
        >Have you tried re-binding the DGV after you have revised the datasource?
        >Also, Datatable has a DefaultView property which sports a Sort property
        >that
        >you can set, and then bind to the View instead of the table.
        >Peter
        >>
        >--
        >Site: http://www.eggheadcafe.com
        >UnBlog: http://petesbloggerama.blogspot.com
        >Short urls & more: http://ittyurl.net
        >>
        >>
        >>
        >>
        >"sklett" wrote:
        >>
        >>(I posted this in a databinding NG, but it's a VERY low traffic NG so I
        >>thought I would post here as well. I hope no one minds too much, if
        >>you do
        >>I'm sorry)
        >>>
        >>I have a DGV that is bound to a DataTable that is loaded during
        >>application
        >>startup. I can sort with all the columns and everything works fine. I
        >>have
        >>a method that can be invoked from a context menu that will loop through
        >>the
        >>rows of the bound table and modify a specific cell in each row. After
        >>modifying the rows sorting doesn't work for that column anymore.
        >>>
        >>To give a more familiar example:
        >>Load the Products table from the Northwind DB
        >>Bind to a DGV via a BindingSource object
        >>Test teh sorting by clicking on the DGV columns
        >>add a button and in the click handler, loop through the DataRow
        >>collection
        >>and set the ProductID column to the value using an int that increments
        >>in
        >>the loop.
        >>Try and sort the ProductID column.
        >>>
        >>You will get incorrect results, it will sort 20 or so rows, then jump
        >>to
        >>another number, then jump again.. it's broken.
        >>>
        >>I've tried everything I can think of:
        >>calling EndEdit on each row
        >>reseting bindings
        >>etc
        >>>
        >>Nothing seems to solve it. I'm not one to cry "bug" but this sure
        >>seems
        >>like one to me.
        >>>
        >>Anyone ever experience this? Anyone happen to have a demo application
        >>setup
        >>with Northwind they could quickly check this? I've spent over a week
        >>now
        >>trying to find the problem and I can't. :(
        >>>
        >>Any help or input greatly appreciated!
        >>>
        >>Thanks for reading,
        >>Steve
        >>>
        >>>
        >>>
        >>>
        >
        >

        Comment

        • sklett

          #5
          Re: Very odd Sort problem when modifying DGV bound data

          Hi Robin,

          Thank you for the reply. I have tried a BindingSource, it didn't make a
          difference, same exact behavior :(

          Have a good weekend,
          Steve


          "RobinS" <RobinS@NoSpam. yah.nonewrote in message
          news:NKqdnU9rs6 lJPpnbnZ2dnUVZ_ veinZ2d@comcast .com...
          If you are using .Net 2.0, you might try putting a BindingSource between
          your data source and your DGV, and using that to sort.
          >
          BindingSource myBindingSource = new BindingSource() ;
          myBindingSource .DataSource = myDataTable;
          myDataGridView. DataSource = myBindingSource ;
          >
          Any changes you make to the underlying data source are immediately
          displayed to the bound control via the BindingSource. It's kind like
          lubrication in a bunch of gears. And you can do sorts and filters and all
          kinds of things with the BindingSource.
          >
          Robin S.
          --------------------------------------
          "sklett" <s@s.comwrote in message
          news:ObHA2NabHH A.4520@TK2MSFTN GP06.phx.gbl...
          >Thank you for the reply Peter,
          >>
          >Yes, my mistake, I am binding to the DataTable.Defau ltView and also
          >specifying the Sort string via the DataTable.Defau ltView.Sort property.
          >>
          >I'm checked the the DefaultView is sorting correctly and it is, if I
          >print out the values to the console everything is correct, but once the
          >DGV displays the items it makes a mess of it all.
          >very strange.
          >>
          >>
          >"Peter Bromberg [C# MVP]" <pbromberg@yaho o.yabbadabbadoo .comwrote in
          >message news:CAC49CBE-2C1C-4B24-807E-97962EDBBC10@mi crosoft.com...
          >>Have you tried re-binding the DGV after you have revised the datasource?
          >>Also, Datatable has a DefaultView property which sports a Sort property
          >>that
          >>you can set, and then bind to the View instead of the table.
          >>Peter
          >>>
          >>--
          >>Site: http://www.eggheadcafe.com
          >>UnBlog: http://petesbloggerama.blogspot.com
          >>Short urls & more: http://ittyurl.net
          >>>
          >>>
          >>>
          >>>
          >>"sklett" wrote:
          >>>
          >>>(I posted this in a databinding NG, but it's a VERY low traffic NG so I
          >>>thought I would post here as well. I hope no one minds too much, if
          >>>you do
          >>>I'm sorry)
          >>>>
          >>>I have a DGV that is bound to a DataTable that is loaded during
          >>>applicatio n
          >>>startup. I can sort with all the columns and everything works fine. I
          >>>have
          >>>a method that can be invoked from a context menu that will loop through
          >>>the
          >>>rows of the bound table and modify a specific cell in each row. After
          >>>modifying the rows sorting doesn't work for that column anymore.
          >>>>
          >>>To give a more familiar example:
          >>>Load the Products table from the Northwind DB
          >>>Bind to a DGV via a BindingSource object
          >>>Test teh sorting by clicking on the DGV columns
          >>>add a button and in the click handler, loop through the DataRow
          >>>collection
          >>>and set the ProductID column to the value using an int that increments
          >>>in
          >>>the loop.
          >>>Try and sort the ProductID column.
          >>>>
          >>>You will get incorrect results, it will sort 20 or so rows, then jump
          >>>to
          >>>another number, then jump again.. it's broken.
          >>>>
          >>>I've tried everything I can think of:
          >>>calling EndEdit on each row
          >>>reseting bindings
          >>>etc
          >>>>
          >>>Nothing seems to solve it. I'm not one to cry "bug" but this sure
          >>>seems
          >>>like one to me.
          >>>>
          >>>Anyone ever experience this? Anyone happen to have a demo application
          >>>setup
          >>>with Northwind they could quickly check this? I've spent over a week
          >>>now
          >>>trying to find the problem and I can't. :(
          >>>>
          >>>Any help or input greatly appreciated!
          >>>>
          >>>Thanks for reading,
          >>>Steve
          >>>>
          >>>>
          >>>>
          >>>>
          >>
          >>
          >
          >

          Comment

          Working...