cancel
Showing results for 
Search instead for 
Did you mean: 

How to count the number of selected rows in a grid?

leon_laikan
Participant
0 Kudos

Hi,

I have a grid with one column of checkboxes. I use VB.NET.

During runtime, as the user ticks each checkbox, I would like the number of selected rows (i.e checked) to be displayed on the status bar.

I've read that we can use the foll. command to count the no. of rows with a ticked checkbox, but could not make it work:

oGrid.Rows.SelectedRows.Count

There are very few posts on this topic on the forum, and SDK help is very sparse.

Can anybody help?

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

pedro_magueija
Active Contributor
0 Kudos

Hi Leon,

SelectedRows gives you the number of rows which were selected by the user (usually they become orange). If you want to count the number of rows with the checkbox checked, the best option would be to export to xml and use a simple Xpath expression to count the rows with Y the specific datatable column.

The other alternative (not as efficient when you have a lot of rows) is to loop through the rows (in the datatable) and count the ones with Y in the specific column.

I'd recommend the xml approach.


Best regards,

Pedro Magueija


View Pedro Magueija's profile on LinkedIn

leon_laikan
Participant
0 Kudos

Hi Pedro,

Thanks a lot for your reply.

After reading it, I am not sure I understand what "selected row" means.

When a user ticks a checkbox, does it mean the row is selected?

I have never seen a row become orange. Can you give an example how a user would select a row?

I know how to count the total number of ticks by looping.

For my current project, what I wish to achieve is this:

As the user ticks the checkboxes one by one, the status bar displays the total no. of ticks.

So, the no. on the status bar is increasing while the user add ticks, and decreasing if he unticks.

Just to indicate to the user how many ticks he has placed.

Can this be done?

What are the main steps to do this?

Best Regards

Leon Lai

pedro_magueija
Active Contributor
0 Kudos

Hi Leon,

No, having a checkbox does not mean the row is selected (at least as far as the grid is concerned).

Selecting a row usually makes look like this (the orange row is selected):

If you just want to count the number of checked checkboxes, you can loop through the datatable rows using the datatable.GetValue() method to get the value of the checkbox column. If the value is "Y" then you increment some counter.


Best regards,

Pedro Magueija


View Pedro Magueija's profile on LinkedIn

leon_laikan
Participant
0 Kudos

Oh! I see.

I completely misunderstood the meaning of "selected row"

I do not want to generate the total no. of rows at the end of a process.

Rather, I would want the running total no. of rows to be displayed on the status bar as and when the user ticks a checkbox.

Can this be done?

Thanks

Leon

pedro_magueija
Active Contributor
0 Kudos

Hi Leon,

That is different, but yes it can be done. Capture the ItemPressed event on your grid and check if it belongs to the checkbox column. If it is for the column you need increment some counter (which you use to also update the status bar).

Capturing event for the grid is the same as you've done but you should check also the pVal.ColID which will tell in which column the event occurred.


Best regards,

Pedro Magueija


View Pedro Magueija's profile on LinkedIn

leon_laikan
Participant
0 Kudos

Hi Pedro

Thanks a lot.

I will try on Monday.

Best Regards,

Leon Lai

Answers (0)