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. Deleting a Row from a DataSet in a DataGrid

Deleting a Row from a DataSet in a DataGrid

Scheduled Pinned Locked Moved C#
databasehelp
4 Posts 3 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.
  • J Offline
    J Offline
    joschmo80
    wrote on last edited by
    #1

    Here is what I am trying to do. I have a DataSet that is displayed in a DataGrid. I would like the user to be able to highlight a row and then press the delete button above it, and have the highlighted row be deleted. I have somewhat accomplished this. My current method is to search the DataGrid for the selected rows and store their index number. Then delete the rows with the corresponding index in the DataSet. This works great as long as the DataSet was not sorted at all while in the DataGrid. If the data was sorted then the index's of the DataGrid are still in ascending order (i.e. 0 for the top row, 1, ...), but the index's of the DataSet are in the order that the row was added. Thus, I delete the wrong row. The code that I have is below, any help is appreciated. ArrayList rowsToRemove = new ArrayList(); for(int i = 0; i < myDataSet.Tables[myTableName].Rows.Count; i++) { if(myDataGrid.IsSelected(i)) { rowsToRemove.Add(i); } } int count = 0; foreach (object j in rowsToRemove) { myDataSet.Tables[myTableName].rowsToRemove[(int)j - count].Delete(); count ++; }

    D H 2 Replies Last reply
    0
    • J joschmo80

      Here is what I am trying to do. I have a DataSet that is displayed in a DataGrid. I would like the user to be able to highlight a row and then press the delete button above it, and have the highlighted row be deleted. I have somewhat accomplished this. My current method is to search the DataGrid for the selected rows and store their index number. Then delete the rows with the corresponding index in the DataSet. This works great as long as the DataSet was not sorted at all while in the DataGrid. If the data was sorted then the index's of the DataGrid are still in ascending order (i.e. 0 for the top row, 1, ...), but the index's of the DataSet are in the order that the row was added. Thus, I delete the wrong row. The code that I have is below, any help is appreciated. ArrayList rowsToRemove = new ArrayList(); for(int i = 0; i < myDataSet.Tables[myTableName].Rows.Count; i++) { if(myDataGrid.IsSelected(i)) { rowsToRemove.Add(i); } } int count = 0; foreach (object j in rowsToRemove) { myDataSet.Tables[myTableName].rowsToRemove[(int)j - count].Delete(); count ++; }

      D Offline
      D Offline
      DougW48
      wrote on last edited by
      #2

      Why can't you just use the dataAdapter's update method?

      1 Reply Last reply
      0
      • J joschmo80

        Here is what I am trying to do. I have a DataSet that is displayed in a DataGrid. I would like the user to be able to highlight a row and then press the delete button above it, and have the highlighted row be deleted. I have somewhat accomplished this. My current method is to search the DataGrid for the selected rows and store their index number. Then delete the rows with the corresponding index in the DataSet. This works great as long as the DataSet was not sorted at all while in the DataGrid. If the data was sorted then the index's of the DataGrid are still in ascending order (i.e. 0 for the top row, 1, ...), but the index's of the DataSet are in the order that the row was added. Thus, I delete the wrong row. The code that I have is below, any help is appreciated. ArrayList rowsToRemove = new ArrayList(); for(int i = 0; i < myDataSet.Tables[myTableName].Rows.Count; i++) { if(myDataGrid.IsSelected(i)) { rowsToRemove.Add(i); } } int count = 0; foreach (object j in rowsToRemove) { myDataSet.Tables[myTableName].rowsToRemove[(int)j - count].Delete(); count ++; }

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        You should use the DataGrid.BindingContext to get a CurrencyManager, which is always the view of the bound DataTable. That means if you sort it, the CurrencyManager will enumerate the rows in the sorted order. This has been discussed before and I've posted an example several times. Please click "Search comments" above this message board and search for "CurrencyManager". You can also view the documentation for the CurrencyManager example in the .NET Framework SDK, which is installed by default with VS.NET, available as a separate download from http://msdn.microsoft.com/netframework[^], and available to read online at http://msdn.microsoft.com/library[^]. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        J 1 Reply Last reply
        0
        • H Heath Stewart

          You should use the DataGrid.BindingContext to get a CurrencyManager, which is always the view of the bound DataTable. That means if you sort it, the CurrencyManager will enumerate the rows in the sorted order. This has been discussed before and I've posted an example several times. Please click "Search comments" above this message board and search for "CurrencyManager". You can also view the documentation for the CurrencyManager example in the .NET Framework SDK, which is installed by default with VS.NET, available as a separate download from http://msdn.microsoft.com/netframework[^], and available to read online at http://msdn.microsoft.com/library[^]. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          J Offline
          J Offline
          joschmo80
          wrote on last edited by
          #4

          Thanks! Your response was most helpful, and your right, that did the trick!

          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