Re: Datagrid multiple row selections
- From: "eman2005" <eman2005@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 6 Apr 2005 07:45:13 -0700
Dear Mark
Thank you again for your valuable feedback.
I think by now I can manage my problem, with less pain ;-)
Just for the info, items could be common in more than one group, in my case.
They rae simply elements of dental treatment, you see.
I wish there was a way to copy an entire record into another recordset,
rather than field by field.
Well, anyway, many thanks.
Eman
"Mark J. McGinty" wrote:
>
> "eman2005" <eman2005@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:4D5660CC-4327-4531-A340-7CE0C510E429@xxxxxxxxxxxxxxxx
> > Hi Mark
> > Yes, I was wondering why there was no dim and set.
> > Also I am not familiar with cloning.
> > Gosh, I wish I could clone myself so easily like recordsets can be cloned
> > ;-)
>
> Indeed! :-) Sadly I fear in practice this would open up a whole new realm
> of problems... but I digress.
>
> > This is brilliant, Mark, thank you very much.
> > However, other than delete, suppose I wanted to copy the selected lines to
> > another grid. I guess you have a way to do this too, probably a little
> > work
> > on the same routine?
>
> It's a very similar task, it will involve iterating the SelBookmarks
> collection, and then altering the underlying recordset of the affected
> grids. Copying SelBookmarks to an array is only necessary if you plan to
> alter the recordset for the grid with selected rows. If all you plan to do
> is use that selection to change other grids, you can just use the
> SelBookmarks collection directly.
>
> As for pure cut-and-paste, I implemented it in comma-separated variable for
> interop with Excel, but it sounds like that's not quite what you have in
> mind.
>
> > Namely, I have a list of items from which I need to allow the user to
> > create
> > groups by selecting some items from the list and clicking the Grouping
> > button
> > which would make the selected items get copied (added) to the other
> > datagrid
> > (thus to the other dataset) under a predefined group name.
>
> Not sure what your underlying db is, nor if/how you plan to persist the
> data, nor details of your schema, so it's difficult if not impossible to
> advise on the best way to do this in your case... but here are a copule of
> possibilities:
>
> One is regular old brute force, loop through SelBookmarks, and for each
> selected row in one grid, create a new row in the underlying recordset of
> the other grid, and copy the row from the first field-by-field. That will
> surely work, but some consideration is due to the db tables storing the
> data. Multiple tables storing essentially the same data is likely not the
> most effecient design, and could open up serious query access problems down
> the line.
>
> Another way would assume that the grouping operation does not actually move
> any rows, but instead associates them via a GroupID field (or some such.)
> In this case, given the user has selected the destination group, all you'd
> need to do is change the GroupID field in the selected rows, requery the
> recordset and rebind the grid. The SQL statement would perhaps filter rows
> with a non-null value in GroupID, so that the value change eliminates them
> from the set. The recordset for the destination grid would need some
> corresponding selectivity; it would also need to be requeried and rebound to
> reflect the change.
>
> If your db connection isn't fast, or large number of rows are involved this
> may be impractical. Also there may be 1000 other ways to do this, so you'll
> want to try at least a few different algorithms, benchmark them objectively,
> and pick the best one for you project.
>
> Simple, right? :-)
>
>
> Good Luck,
> Mark
>
>
>
>
> > I greatly appreciate your help.
> >
> > Eman
> >
> >
> > "eman2005" wrote:
> >
> >> Hi Mark
> >> Thank you for your kind reply.
> >> I learned all this stuff on my own, so please bear with me.
> >> I tried your code.
> >> I created a function called DeleteSelectedRows and put your code in it
> >> and
> >> replaced Grid with my grid name grdTrtComp
> >> I also set the grid Allow delete to true.
> >> Then I added:
> >> Private Sub grdTrtComp_Error(ByVal DataError As Integer, Response As
> >> Integer)
> >> Response = 0
> >> If DataError = dbgDelMultRows Then DeleteSelectedRows
> >> End Sub
> >>
> >> Now when I selected two rows and hit the keyboard Delete button, I got an
> >> 'Object not defined' error on this line
> >> rs.ActiveConnection.Errors.Clear
> >>
> >> Other than that, is this correct?
> >> If rs.ActiveConnection.Errors.Count > 0 Then Exit For
> >> or should it be = 0 ?
> >>
> >> Thank you again
> >> Eman
> >>
> >> "Mark J. McGinty" wrote:
> >>
> >> >
> >> > "eman2005" <eman2005@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> >> > news:F1745039-7BF0-444E-9F9B-314E0E978766@xxxxxxxxxxxxxxxx
> >> > > It is possible to press Ctrl and make multiple selections of separate
> >> > > rows
> >> > > (e.g. 2, 3 6, 8, 11, 12,...etc) in a datagrid.
> >> > > That is great, but useless.
> >> > >
> >> > > If I hit the delete button, it prompts that it cannot delete multiple
> >> > > selections.
> >> > >
> >> > > What is the use of allowing multiple selections if you cannot use it
> >> > > to
> >> > > move, copy or delete the selected rows?????
> >> >
> >> > Who says you can't? It's true the grid does not do it for you, but
> >> > this is
> >> > not to say it can't be done.
> >> >
> >> > You must first process the grid's Error event, setting the Response
> >> > parameter to 0 to bypass the default error message. In the same
> >> > handler,
> >> > the attempt to delete multiple rows can be detected based on the error
> >> > value, see the example below (which assumes the grid control is named
> >> > "Grid".)
> >> >
> >> > Private Sub Grid_Error(ByVal DataError As Integer, Response As Integer)
> >> > Response = 0
> >> > If DataError = dbgDelMultRows Then DeleteSelectedRows
> >> > End Sub
> >> >
> >> >
> >> > My handler above calls a function that I wrote to do the job, here it
> >> > is in
> >> > a nutshell:
> >> >
> >> > ' First you'll need to make a clone of the recordset that's bound
> >> > to the
> >> > grid
> >> > ' If you try to use the same recordset, the record pointer changes
> >> > cause
> >> > ' strange behavior. If it's not a clone, the bookmarks will not be
> >> > valid.
> >> > Dim BmkArray()
> >> >
> >> > ' Check to see if multiple rows have been selected, if so allocate
> >> > space
> >> > in
> >> > ' an array and copy the bookmarks. The copy is necessary because
> >> > any
> >> > ' changes to the number of records in the recordset tend to
> >> > screw-up
> >> > ' this collection
> >> > '
> >> > If (Grid.SelBookmarks.Count > 0) Then
> >> > ReDim BmkArray(Grid.SelBookmarks.Count - 1)
> >> >
> >> > For i = Grid.SelBookmarks.Count - 1 To 0 Step -1
> >> > BmkArray(i) = Grid.SelBookmarks(i)
> >> > Next
> >> >
> >> > Else
> >> > ' handle a single selection
> >> > ReDim BmkArray(0)
> >> > BmkArray(0) = Grid.Bookmark
> >> > If (IsNull(BmkArray(0))) Then Exit Sub
> >> > End If
> >> >
> >> > ' Loop through the array and delete the records referenced by the
> >> > bookmarks
> >> > '
> >> > For i = 0 To UBound(BmkArray)
> >> > rs.ActiveConnection.Errors.Clear
> >> > rs.Bookmark = BmkArray(i)
> >> > If rs.ActiveConnection.Errors.Count > 0 Then Exit For
> >> > rs.Delete
> >> > rs.MoveFirst
> >> > Next
> >> >
> >> >
> >> > -Mark
> >> >
> >> >
> >> >
> >> >
> >> > > --
> >> > > Eman FATiH
> >> > > Technical Communicator
> >> > > www.smartistek.com
> >> > >
> >> >
> >> >
> >> >
>
>
>
.
- References:
- Datagrid multiple row selections
- From: eman2005
- Re: Datagrid multiple row selections
- From: Mark J. McGinty
- Re: Datagrid multiple row selections
- From: eman2005
- Re: Datagrid multiple row selections
- From: eman2005
- Re: Datagrid multiple row selections
- From: Mark J. McGinty
- Datagrid multiple row selections
- Prev by Date: Re: Datagrid multiple row selections
- Next by Date: Re: My Database Crash Every Day !..
- Previous by thread: Re: Datagrid multiple row selections
- Next by thread: connection with sql server
- Index(es):