PC Review


Reply
Thread Tools Rate Thread

Deleting rows in datagrid with rowfilter

 
 
PeterB
Guest
Posts: n/a
 
      9th Sep 2004
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


 
Reply With Quote
 
 
 
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      9th Sep 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
PeterB
Guest
Posts: n/a
 
      10th Sep 2004
Exactly!

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

/ Peter


"Alex Feinman [MVP]" <(E-Mail Removed)> skrev i meddelandet
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid deleting rows =?Utf-8?B?cm9kY2hhcg==?= Microsoft VB .NET 2 8th Oct 2004 02:07 PM
Deleting rows from DataGrid =?Utf-8?B?Sm9iIExvdA==?= Microsoft Dot NET 2 15th Jul 2004 03:01 PM
Re: Deleting Rows in datagrid William Ryan eMVP Microsoft C# .NET 4 29th Apr 2004 02:44 PM
RE: deleting mutiple rows in datagrid Ying-Shen Yu[MSFT] Microsoft Dot NET Framework Forms 0 28th Feb 2004 02:30 AM
Deleting rows from a datagrid Junkguy Microsoft C# .NET 1 4th Nov 2003 12:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:27 PM.