Remove a row from a sorted table ?

  • Thread starter Thread starter Amerio
  • Start date Start date
A

Amerio

Hi,
I have a datagrid, sorted according to its 1st column.
I want to remove the row currently selected.
But CurrentRowIndex returns the sorted index, witch is not (!!) the stored
row index.
Any one can help ?
 
The trick to this is figuring out what the datagrid is bound to and how it is bound... There are quite a few different ways t
get this but confusing part is realizing that a datagrid uses some type of BindingManagerBase object, usually a currencyManger for a DataTable and that the Binding Manager contains the current object from the bound source.

Once you have the current object you need to cast it back to its Type and go from there...

I found this very confusing at first..

Here is some quick code to try and explain this...(I didn't test this, just zipped it off from memory but it should give you
an idea..

//DataRow we are going to fin
DataRow dataRow = null
// Approach 1 - very Generi
/
// You need to get the BindingManager Object for the Data Gri
// This is typically either a CurrencyManager o
// a PropertyManager, a Currency Manager will have a list propert
//
// If you use the BindingManagerBase object then you can see wha
// Type the Current item is.
BindingManagerBase bindingManagerBase =
dataGrid.BindingContext[dataGrid.DataSource,dataGrid.DataMember]

// Get the Current Bound Objec
Object currentBoundObject = bindingManagerBase.Current
//Determine what Type of Object you are actually bound to for a sourc
if (currentBoundObject != null

//Most Commonly you will get a DataRowView back as the current ite
if (currentBoundObject.GetType() == typeof(DataRowView)

//To Get the Data Row from Current Bound DataRowView Object
dataRow = ((DataRowView)currentBoundObject).Row


// Removing the DataRow from its Tabl
if (dataRow != null
dataRow.Table.Rows.Remove(dataRow)

/
// Approach 2 Assumes a Currency Manager onl
//
CurrencyManager cm =
(CurrencyManager)dataGrid.BindingContex
[dataGrid.DataSource,dataGrid.DataMember]
as CurrencyManager
//Make sure that there actually was a currency Manager an
//that it has a lis
if (cm != null && cm.List != null

//Check if the list is DataVie
if (cm.List.GetType() == typeof(DataView))

if (cm.Current != null
dataRow = ((DataRowView)cm.Current).Row


//Now do what you want with the data Ro

// Removing the DataRow from its Tabl
if (dataRow != null
dataRow.Table.Rows.Remove(dataRow)
 
Amerio said:
Hi,
I have a datagrid, sorted according to its 1st column.
I want to remove the row currently selected.
But CurrentRowIndex returns the sorted index, witch is not (!!) the stored
row index.
Any one can help ?

Amerio,

I don't know if you are interested, but I thought I would let you know
about our data controls. They are called SQLCafe. You can basically
bind our grid and our controls to tables/columns at design-time.
Also, with our maintenance bar, you can bind it to our data control
and add/change/delete rows with little or no effort at all. Also, in
reference to your problem, when you delete rows with the maintenance
bar, it knows which row to delete even if the grid is sorted. If you
get a chance, download our beta version and try them out. If you have
questions, please email (e-mail address removed) or can login to our
discussion forum and post comments/issues.

Thanks,

(e-mail address removed)
 
Back
Top