I have a project in VB6 and I need help in how to get the number of selected rows in a Datagrid.. I have already tried using datagrid1.selBookmarks.count but it doesnt seem to work.. i have set the marquee style property of the datagrid into highlight rows.
Can someone give me an insight about this..

Dani AI

Generated

A few clarifications and a small, reliable fix that ties together ’s observation and ’s suggestion.

By default the DataGrid only adds rows to its selection collection when the user clicks a record selector (the left-hand arrow). That is why counting the selection collection worked after clicking that button but not after clicking an ordinary cell. To make clicks on any cell behave like clicking the selector, add the current row’s bookmark to the SelBookmarks collection from the grid’s row-change event. (stackoverflow.com)

Example (put this on the form with the DataGrid):

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
With DataGrid1
' clear old selection here if you want single-click = single-select
.SelBookmarks.Add .Bookmark
End With
End Sub

This programmatic approach is exactly what VB-Helper and the DataGrid docs show for selecting rows by bookmark; it makes clicks on cells update the same collection you were trying to count. If you want single-selection behavior, clear the SelBookmarks before adding the new bookmark; if you want Ctrl-click toggling, detect the Ctrl key and add/remove the current bookmark instead. (vb-helper.com)

Quick practical notes: the underlying Recordset must support bookmarks (client-side cursor or bookmarkable provider), otherwise bookmarks/selection will fail. After changing selection in code you may need to Refresh or ReBind the grid in some cases. If fine-grained selectable visuals are needed, consider MSFlexGrid/ListView alternatives for easier custom selection handling. (shabukc.tripod.com)

Recommended Answers

All 5 Replies

If you want to know how many rows are selected then DataGrid1.SelBookmarks.Count works great to me.

its working if im not using "datagrid marquee style= highlight rows".. but i really nid to highlight all the column in a row when someone click on it. should i just use for loop to get the number of rows highlighted? or you have a better idea for this?

its working if im not using "datagrid marquee style= highlight rows".. but i really nid to highlight all the column in a row when someone click on it. should i just use for loop to get the number of rows highlighted? or you have a better idea for this?

It's still works to me.
I already set marquestyle = dbghighlightrow and i still can use DataGrid1.SelBookmarks.Count to get number of selected rows.

it is working but u still need to click the button on the leftside of the datagrid before it could get the number of selected row. In my case i want the user to click any column to higlight the row. can i still get the number of selected row without clicking that button?

appreciate the reply.. thx Jx_Man..

can i still get the number of selected row without clicking that button?

So in any event you want to put it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.