I'm trying to get a list of rows deleted by pressing the '-' button in a Devexpress Grid widget as depicted here.
However, doing the following does not return any results
DataView delrows = new DataView(myTableAdapter.DataView.Table);
delrows.RowStateFilter = DataViewRowState.Deleted;
What am I doing wrong?
edit: Filtering on Added and Modified rows works fine.
instead of doing as you do now try this:
myTableAdapter.DataView.RowStateFilter = DataViewRowState.Deleted;
of course it's not easy to guess without knowing better your data binding architecture...
Considering that your DevExpress grid is binded to a DataTable (either with or without a DataView):
You can retrieve the deleted rows by using the Select() method of the DataTable. This is not a linq method.
table.Select(null, null, DataViewRowState.Deleted);
The rest of the rows can be retrieved by using
table.Select(null, null, DataViewRowState.CurrentRows);
Just be aware that a row that is added then deleted will not have the deleted flag, but will instead be removed from the rows collection. Such rows would also have a RowState of Detached.
Related
In c# how to add new row in to a datagridview after the existing row.
I tried grdshedule.Rows.Add();
But all the rows are adding before the existing row
Make sure that AllowUserToAddRows property of datagridview is False. And like u tried before programmaticly ;
datagridview.Rows.Add();
grdshedule.Rows.Insert(grdshedule.Rows.Count-1, newRow);
Your rows are not getting automatically sorted hence you cannot see the new row at the bottom, this means once you will have to resort your rows after adding new row
To sort please example http://msdn.microsoft.com/en-us/library/0868ft3z.aspx
**Line Taken from MSDN Remarks**
Rows in the control are not automatically sorted when new rows are added.
Please read full remarks from MSDN http://msdn.microsoft.com/en-us/library/x0akdhcd.aspx
I had the same issue with DataGridView when it came to adding a new row after existing rows or at the end of the table.
After spending some time on this, I figured out why I couldn't add rows at the end of the table. It's not that the datagridview had any issues or restrictions, but it was really in my code. However, datagridview does add new row at the end of the table every time.
I really wish you posted some part of your code it will help a lot in giving you an answer.
Anyways, in my program I have a class from which I create list of objects. My List is actually an ArrayList and I display these objects in the same order they are in the list. Whenever I add or delete an object from the List, it is reflected 1-to-1 on the datagridview. In other word, if the objects were listed in reverse order in the ArrayList, then that's the order the objects were shown in the Datagridview. That doesn't mean the rows are being added on top of your existing rows, although it looked like it for me.
My guess is that you have a list of some sort that you are maintaining in your program. This list is where you get your information to display on your datagridview. If I am correct in saying that, then you probably are inserting your items in your list somewhere in your program based on the row that is currently selected on your datagridview. If that is the case, then your current item is being inserted into the position of your selected items in your ArrayList. This causes your list to shift everything by one from the selected item in your ArrayList. Thus, when you display your list of items in your datagridview, it looks like the new row is not being added at the end of the table.
To resolve this issue, first you need to add a new empty row:
DGrid.Rows.Add;
Then, insert your new item at the very end of your list:
var newitem := new Object;
ArrayList.insert(DGrid.RowCount-1,newitem);
Then, update your datagridview based on this ArrayList. Now, your datagridview will act as you expected.
Before my fix, my datagridview kept acting like it was inserting new rows on the top and in reverse order.
I have a DataView called FubarView, which was created by a call to our database. The columns are Label, Value, RawName & PhoneNumber. After creation of the DataView, I added a sort order to the DataView with...
this.FubarView.Sort = "RawName, Value"
I then (amongst other irrelevant stuff like setting DisplayMember, etc.) bound it to my WinForms ComboBox...
cmbDefault.DataSource = this.FubarView;
This worked perfectly, with the ComboBox, displaying sorted information as intended. HOWEVER, when at a later point I tried to look at FubarView using the SelectedIndex from my ComboBox...
phoneNumber = this.FubarView.Table.Rows[cmbDefault.SelectedIndex]["PhoneNumber"]
...it would return the wrong value, as if FubarView went and sorted itself by Value again! How do you fix this?
This is because you are sorting a view on the table, not the actual table. So if you access the Table trough DataView.Table you get your original data.
If you want to access the sorted rows you should access them trough the DataView.
I wouldn't work with indexes, I would use IDs instead.
I have a DataTable (displayed via a DataGridView) and want to reuse it to manually fill in different data. So the workflow is:
Every time I need to refill the DataTable, I do:
Unhook the DataTable from the DataGridView.
Clear DataTable content.
Manually add DataColumns and DataRows to the DataTable.
Hook the DataTable again with the DataGridView.
To achieve step 0, I simply do: DataGridView.DataSource = null.
To achieve step 1, I call DataTable.Clear(), DataTable.Rows.Clear() and DataTable.Columns.Clear().
To achieve step 3, I call DataGridView.DataSource = DataTable.
Step 2 is done in normal way and I skip the code.
Whenever the DataGridView is updated with a new content, if I click DataGridView column to sort, the next time when the DataTable is updated, I will get a null reference exception in step 2 when I try to add new rows. But if I never click DataGridView column to sort, all works fine.
I guess it has something to do with the sorting. But I have no idea what causes the null reference exception. Is it possible that the DataTable tries to search some keys in old content but couldn't find it? It cannot find the old content because of step 1 of course. What is the right way of clearing old DataTable content so it never refers the old content when I fill in new stuff?
Thanks.
The DataGridView's databinding is tied to the table (through a DataView); you cannot pull the table out from under it and expect it to continue working.
When you sort by a column, the grid asks the DataView to change its sort order, which fails because the column doesn't exist anymoe.
You should create a new DataTable.
In my code I need to remove rows from the DataGridView after a recurring interval, and so I call the following function when a timer expires:
private void removeRows(DataGridView dgv) {
foreach (DataGridViewRow row in dgv.Rows)
{
// if some condition holds
dgv.Remove(row);
}
dgv.Refresh();
}
I know the rows are successfully deleted from the DataGridView, though they still remains in the display for whatever reason. Any tips on what I might be doing wrong?
Don't you need to rebind the data grid?
dgrv.Datasource = [whatever data source];
dgrv.DataBind();
?
Sometimes refreshing the data gridview is not enough and its containing parent should be refreshed too.
Try this:
dgv.Refresh(); // Make sure this comes first
dgv.Parent.Refresh(); // Make sure this comes second
You could also edit your source and attach the new datasource to the control.
this code could be useful:
dataGridView.DataSource = null;
dataGridView.Update();
dataGridView.Refresh();
dataGridView.DataSource = SomeDataSource;
Hope this helps.
If you have bound your datagrid to an Observable Collection (if not then you should) then you will need to implement INotifyCollectionChanged interface so that listeners are notified of dynamic changes, such as when items get added and removed or the whole list is refreshed.
HTH
If I understand you correctly, you want to delete rows selected by a user from your DGV.
Use the DataGridViewRowCollection of your DGV rather than the DataRowCollection of the DataTable. The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise.
Once you have determined that a row is to be deleted, you can use the Remove method of the DataGridViewRowCollection to delete the item from the grid, e.g. YerDataGridView.Rows.Remove(row)
Note that at this point, although the item is removed from the DGV, it still has not been deleted from the Access DB. You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, e.g. YerTableAdapter.Update(YerDataSet)
I normally would call Update once to commit the changes only after having removed all the items to be deleted from the DGV.
If it's a data-bound grid, you should be working on the binding source itself instead of the grid.
Try removing the actual items from your binding source instead.
I had same problem and I have find out the root cause.
all you have to do initialize you database context object in before reload and see the magic .
How to Add Number Rows to a grid view dynamically using c# asp.net
Add new "rows" to the datasource of the grid. If your datasource is a DataTable, then add a new DataRow to the DataTable. If your datasource is a collection of objects, then add a new object to that collection.
Don't forget to rebind the grid once you have updated the datasource.
To do this, add rows to the data source to which the grid is bound.
Here's an example.
Edit: The link I posted adds a row without adding to the data source. It seems kind of kludgey to me, but it may be more like what you're looking for.