Is there some way other than IsSelected in a DataGrid?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

Hi,

I have been through all kinds of documentation on the datagrid and
dataviews. I have many expensive books that I have looked through. But
nothing has really helped. I want to find out and retrieve copies of
any selected records that are in a dataview. One thing I have done is
used the MouseDown event and HitTestInfo to find out what record has
been clicked on. This tells me the current record and I can retrieve
the record and add it to my collection . But what if a user holds the
Shift key down to select multiple records? What if they only use the
keyboard and bypass the mouse altogether?

I need to know what records are <B>selected</B> by the user and then
retrieve copies of them. I know of the DataGrid.IsSelected(index)
function, but what if the datagrid has a million records? I know that
sounds like an impractical amount, but I don't know how many records
will be returned by a query. I have to take a million record
possibility into account. And some of our tables have more than a
million records.

IsSelected() is not very practical when there are a lot of records. Is
there some other way?

One other thing. What if I want to get the selected DataRowView from
the dataview and then add it to a data table? I get errors saying that
you can't convert the DataRowView to a DataRow. This is even when the
schema for the DataRowView is exactly the same as the DataRow and
DataTable.

Thanks for any help on this,

Tom Graves
 
Hello Tom,

I feel your pain but alas, there is NO easy way. You should tap into all the
mouse and keyboard handling to maintain a collection of selected rows
manually. This is tedious, but not impossible. First, inherit from the
DataGrid class and override its OnMouseDown and OnMouseUp methods to handle
the mouse. You can detect Shift in these methods by checking the static
Control.ModifierKeys property. Then, override the
ProcessDialogKey, ProcessCmdKey and ProcessKeyPreview methods (there may be
more methods to override, please check the MSDN on the DataGrid's protected
methods). You should arrange some experiments to find out which override is
best. For me, these were ProcessDialogKey (called when a cell is not being
edited) and ProcessKeyPreview (called when a cell IS being edited).

As an alternative solution, you can search for controls based on the
DataGrid (there definitely is a free one created by a Russian guy, it is
called SuperGrid as far as I remember), or rather switch to a commerial grid
such as ComponentOne or SyncFusion's.
 
Thanks Dmitriy,

I already have some of the code in the mouse down event that detects
if a modifier key was pressed but I was unaware of the other events. I
will have to put code in those other events to capture the record. I
think I will also look at those third party controls as this seems
like a lot of trouble for the most basic functionality from a
datagrid.

Thanks for the suggestions,
Tom
 
Back
Top