This is a migrated thread and some comments may be shown as answers.

RadGrid - Using a checkbox to add rows to the selectedrows collection

1 Answer 96 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 31 Jul 2013, 05:44 PM
I've been reading through the tutorials and forums to see if this is possible or if maybe my approach is wrong. Here's what i am doing.

I am using a pretty simple grid with an id number in the second column.  The first column is a checkbox cell.  All I want to do is to check the checkbox of say 4 or 5 of the rows and them ask the grid which rows have been selected.  Does the grid's selected rows collection maintain this for me or do I have to code this myself to store the key's seperately, which is easy enough to do.  But from looking at the various forum threads, it looks like the grid will do this if you allow mulitple row selections.  But when I set this and check the selectedrows collection, it is always empty.

This sounds like a VERY common question and approach, but I cannot find the anwser.  What I would like to do is something like the following.  This doesn't work because the selectedrows(0) is always empty.  In fact, when I view just the selectedrows, the count is always 0.  So the grid is not keeping track of this for me.

I do have the option turned on to select multiple rows.  I can add my own logic to track this, but I think the grid is handling it based on other threads and the sample code below. 

Private Sub dgvLoanRestore_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dgvLoanRestore.ValueChanged
            dgvLoanRestore.EndEdit()
  
            Dim selectedRows As New List(Of GridViewDataRowInfo)
            Dim item As Object = dgvLoanBackup.SelectedRows(0).DataBoundItem
            selectedRows.Clear()
            selectedRows.Add(item)
  
            ' Recount rows selected (i.e. the number of checked checkboxes in the grid)
            lblSelectedRowCount.Text = selectedRows.Count.ToString()
        End Sub




1 Answer, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 02 Aug 2013, 11:57 AM
Hi Jon,

Thank you for writing.

The CheckBox in the cells in RadGridView display just one value (true or false) and this value does not have relation with the selection of the row. So, it is normal the SelectedRows collection of RadGridView to be empty in this case. However, if you want to know which rows are selected you can use the following approach:
    If TypeOf Me.radGridView1.ActiveEditor Is RadCheckBoxEditor Then
        Me.radGridView1.EndEdit()
    End If
 
    Dim counter As Integer
    For Each rowInfo As GridViewRowInfo In radGridView1.Rows
        If rowInfo.Cells(0).Value = True Then
            counter = counter + 1
        End If
    Next
    MessageBox.Show(counter.ToString())
End Sub

Attached is the demo project that demonstrates this approach in action.

I hope this helps.

Regards,
Anton
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Jon
Top achievements
Rank 1
Answers by
Anton
Telerik team
Share this question
or