deleting selected rows in a DataGrid

  • Thread starter Thread starter Mojtaba Faridzad
  • Start date Start date
M

Mojtaba Faridzad

Hi,

with SetDataBinding( ) a DataGrid shows a DataView. user can select some
rows in the grid by holding cotrol key. when user clicks on Delete button, I
should delete all selected rows. I am trying to delete these lines from the
dataview like this:

for (int i=0; i < dataView.Count; i++)
if (dataGrid.IsSelected(i))
dataView.Delete(i);

but after deleting the first line, all other selected rows, become
unselected and I cannot delete the rest of selected rows. how can I ask
DataGrid not to reset the status of rows?

thanks
 
how about desselecting it first before deleting, :p. If that doesn't work,
i'll go for a temporary storage...
 
Hi,

You can create an ArrayList in which the indexes of the selected rows will
be accumulated. Then, unselect all rows and use the prepared list of indexes
to delete the rows from the data view.
 
Thanks Joey and Dmitriy. I have to delete the selected records in DataSet
too. so it's easier for me to delete the records in DataSet, then reset the
DataView and show the new set in DataGrid. but I am looking to find an event
to control Delete button. then I can let DataGrid to delete the records by
selecting them in the grid and press on <Delete> key. before deleting I have
to delete the records in DataSet too. but I could not find the suitable
event. I tried to use KeyDown event in DataGrid or Form but it seems like
another event happens. Do you know how I can detect Delete key in DataGrid?
 
Hi,

You should inherit from the DataGrid class and override the ProcessCmdKey
and PreProcessMessage methods. You will have to deal with API-level messages
there, so make sure you understand the WM_KEYDOWN and WM_SYSKEYDOWN Windows
messages.

There are a couple of others: ProcessDialogKey and ProcessKeyPreview, but as
far as I remember they are not triggered by pressing the Del key.
 
Dmitriy,

thanks for your help but I don't know I can do it or not :) this is my
second week that I have started to code C# :) more over, if I use API
functions, then my code won't be in safe mode anymore, right? but I like
your solution and I keep it. I will work on that to learn more. Thanks again
Dmitriy

is there any way to know which class handled an event? it helps me to know
which method I should override.


Dmitriy Lapshin said:
Hi,

You should inherit from the DataGrid class and override the ProcessCmdKey
and PreProcessMessage methods. You will have to deal with API-level
messages
there, so make sure you understand the WM_KEYDOWN and WM_SYSKEYDOWN
Windows
messages.

There are a couple of others: ProcessDialogKey and ProcessKeyPreview, but
as
far as I remember they are not triggered by pressing the Del key.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Mojtaba Faridzad said:
Thanks Joey and Dmitriy. I have to delete the selected records in
DataSet
too. so it's easier for me to delete the records in DataSet, then reset the
DataView and show the new set in DataGrid. but I am looking to find an event
to control Delete button. then I can let DataGrid to delete the records
by
selecting them in the grid and press on <Delete> key. before deleting I have
to delete the records in DataSet too. but I could not find the suitable
event. I tried to use KeyDown event in DataGrid or Form but it seems like
another event happens. Do you know how I can detect Delete key in DataGrid?
 
Back
Top