Deleting a Row from a DataSet in a DataGrid
-
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 ++; }
-
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 ++; }
-
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 ++; }
You should use the
DataGrid.BindingContext
to get aCurrencyManager
, which is always the view of the boundDataTable
. That means if you sort it, theCurrencyManager
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 theCurrencyManager
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] -
You should use the
DataGrid.BindingContext
to get aCurrencyManager
, which is always the view of the boundDataTable
. That means if you sort it, theCurrencyManager
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 theCurrencyManager
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]