Deleting rows in datagrid with rowfilter

P

PeterB

Hello!

If I have a datatable with a rowfilter active, the row index of the current
selected row in the DataGrid may not be the same as the row's index in the
DataTable.

I started writing this message because I didn't know how to delete a
selected row in a datagrid when a rowfilter was applied. But I know have
atleast an suggestion (working), and would like to hear others opinion on
this.

In all the documents I have read which uses RowFilter it seems the primary
use of it would be in a "see but don't touch" context. The same goes with
documents about editing datatables and dataviews where I haven't seen any
examples or discussions where RowFilters are in use when editing the tables.

Here is a small example (RowFilter="id>10"):
myDataTable: myDataGrid
row name id row name id
0 Peter 02 0 John 53
1 John 53 1 Peter 25
2 Chris 07
3 Peter 25

If I click John in myDataGrid I have myDataGrid.CurrentRowIndex (rowindex) =
0, but the actual rowindex in myDataTable is 1 for this row. So direct
access to the myDataTable is not recommended as we have a mismatch in
rowindexes here. If I want to edit or remove John, and called
myDataTable.Rows[rowindex]["ColumnName"] = "NewValue" or
myDataTable.Rows.RemoveAt( rowindex ), I would change/delete Peter 02.

This message actually started as a question on how to find the rowindex of
the DataTable given the current selected rowindex in a datagrid (still
unsolved), without using the values of the datarow cells (i.e. primary
keys), which would be an alternative solution to that below.

Instead, if I want to modify the SELECTED row I can use
myDataTable.DefaultView[rowindex]["ColumnName"] = "NewValue", and if I want
to remove the selected row (i.e. John) I can call
myDataTable.DefaultView.Delete(rowindex). (Note. that I use
table.DefaultView but the actual view in use could be different.).

My question is now, is this how YOU do it? If no, how do you do it?
Is there an easy way of getting the rowindex of the underlying table when
selecting a row/cell in a datagrid?

Comments, ideas, and flames are all welcome :)

regards,

Peter
 
A

Alex Feinman [MVP]

The DataGrid is always bound to a DataView, even when you set the source to
a DataTable. The grid row index is always an index in the DataView's array
of DataRowView. So, to get to the row from the grid row index you use

(dataGrid.DataSource as DataView)[gridRowIndex].Row

This gives you a DataRow to manipulate (delete, update etc). This will work
regradless of Sort of RowFilter being applied as the grid is bound to the
View as it looks after applying constraints

--
Alex Feinman
---
Visit http://www.opennetcf.org
PeterB said:
Hello!

If I have a datatable with a rowfilter active, the row index of the
current
selected row in the DataGrid may not be the same as the row's index in the
DataTable.

I started writing this message because I didn't know how to delete a
selected row in a datagrid when a rowfilter was applied. But I know have
atleast an suggestion (working), and would like to hear others opinion on
this.

In all the documents I have read which uses RowFilter it seems the primary
use of it would be in a "see but don't touch" context. The same goes with
documents about editing datatables and dataviews where I haven't seen any
examples or discussions where RowFilters are in use when editing the
tables.

Here is a small example (RowFilter="id>10"):
myDataTable: myDataGrid
row name id row name id
0 Peter 02 0 John 53
1 John 53 1 Peter
25
2 Chris 07
3 Peter 25

If I click John in myDataGrid I have myDataGrid.CurrentRowIndex (rowindex)
=
0, but the actual rowindex in myDataTable is 1 for this row. So direct
access to the myDataTable is not recommended as we have a mismatch in
rowindexes here. If I want to edit or remove John, and called
myDataTable.Rows[rowindex]["ColumnName"] = "NewValue" or
myDataTable.Rows.RemoveAt( rowindex ), I would change/delete Peter 02.

This message actually started as a question on how to find the rowindex of
the DataTable given the current selected rowindex in a datagrid (still
unsolved), without using the values of the datarow cells (i.e. primary
keys), which would be an alternative solution to that below.

Instead, if I want to modify the SELECTED row I can use
myDataTable.DefaultView[rowindex]["ColumnName"] = "NewValue", and if I
want
to remove the selected row (i.e. John) I can call
myDataTable.DefaultView.Delete(rowindex). (Note. that I use
table.DefaultView but the actual view in use could be different.).

My question is now, is this how YOU do it? If no, how do you do it?
Is there an easy way of getting the rowindex of the underlying table when
selecting a row/cell in a datagrid?

Comments, ideas, and flames are all welcome :)

regards,

Peter
 
P

PeterB

Exactly!

But from the MSDN docs this never really "comes out". Alteast not the ones I
browsed..

/ Peter


Alex Feinman said:
The DataGrid is always bound to a DataView, even when you set the source to
a DataTable. The grid row index is always an index in the DataView's array
of DataRowView. So, to get to the row from the grid row index you use

(dataGrid.DataSource as DataView)[gridRowIndex].Row

This gives you a DataRow to manipulate (delete, update etc). This will work
regradless of Sort of RowFilter being applied as the grid is bound to the
View as it looks after applying constraints

--
Alex Feinman
---
Visit http://www.opennetcf.org
PeterB said:
Hello!

If I have a datatable with a rowfilter active, the row index of the
current
selected row in the DataGrid may not be the same as the row's index in the
DataTable.

I started writing this message because I didn't know how to delete a
selected row in a datagrid when a rowfilter was applied. But I know have
atleast an suggestion (working), and would like to hear others opinion on
this.

In all the documents I have read which uses RowFilter it seems the primary
use of it would be in a "see but don't touch" context. The same goes with
documents about editing datatables and dataviews where I haven't seen any
examples or discussions where RowFilters are in use when editing the
tables.

Here is a small example (RowFilter="id>10"):
myDataTable: myDataGrid
row name id row name id
0 Peter 02 0 John 53
1 John 53 1 Peter
25
2 Chris 07
3 Peter 25

If I click John in myDataGrid I have myDataGrid.CurrentRowIndex (rowindex)
=
0, but the actual rowindex in myDataTable is 1 for this row. So direct
access to the myDataTable is not recommended as we have a mismatch in
rowindexes here. If I want to edit or remove John, and called
myDataTable.Rows[rowindex]["ColumnName"] = "NewValue" or
myDataTable.Rows.RemoveAt( rowindex ), I would change/delete Peter 02.

This message actually started as a question on how to find the rowindex of
the DataTable given the current selected rowindex in a datagrid (still
unsolved), without using the values of the datarow cells (i.e. primary
keys), which would be an alternative solution to that below.

Instead, if I want to modify the SELECTED row I can use
myDataTable.DefaultView[rowindex]["ColumnName"] = "NewValue", and if I
want
to remove the selected row (i.e. John) I can call
myDataTable.DefaultView.Delete(rowindex). (Note. that I use
table.DefaultView but the actual view in use could be different.).

My question is now, is this how YOU do it? If no, how do you do it?
Is there an easy way of getting the rowindex of the underlying table when
selecting a row/cell in a datagrid?

Comments, ideas, and flames are all welcome :)

regards,

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top