Question Controlling selection of datagrid rows

nyarlathotep

Member
Joined
Mar 23, 2012
Messages
6
Programming Experience
1-3
Hi again.

In an ideal world I'd like to present a datagrid to users which allows multi-selection but contains some rows they can't select. This is an extension of the multigrid custom control I got working with help here.

The user want to be able to see all the values in the underlying data table, but I want to prevent them from adding certain rows to the selected rows collection, and pop up a message box to explain why they can't.

Google suggests this is not at all easy, but that I might make some progress overriding the setselectedrowcore method. Is it simply a case of code like this, or it this approach a terrible idea?

VB.NET:
    Protected Overrides Sub SetSelectedRowCore(rowIndex As Integer, selected As Boolean)


        If (test on row contents) then


            MsgBox("You can't do that")
            Exit Sub

        Else

            MyBase.SetSelectedRowCore(rowIndex, selected)

        End If

    End Sub
 
First things first, SetSelectedRowsCore is a member of the DataGridView class, so you obviously have a DataGridView and not a DataGrid.

As for the question, I haven't tested anything but it looks like it's on the right track to me. Two things to note though:

1. That Exit Sub is pointless because there's no more code to execute after that line anyway. Even if there was, it is preferred to use Return over Exit Sub.
2. Rather than doing nothing in the first case, I would suggest calling SetSelectedRowCore and passing False as the second argument.
 
Some progress

First things first, SetSelectedRowsCore is a member of the DataGridView class, so you obviously have a DataGridView and not a DataGrid.

As for the question, I haven't tested anything but it looks like it's on the right track to me. Two things to note though:

1. That Exit Sub is pointless because there's no more code to execute after that line anyway. Even if there was, it is preferred to use Return over Exit Sub.
2. Rather than doing nothing in the first case, I would suggest calling SetSelectedRowCore and passing False as the second argument.

Having an attack of the stupids here. I really want to add code like this to the form to override SetSelectedRowsCore for a specific instance of a custom contdatagridview called "source". I had imagined I could do this to achieve that, but visual studio throws an error "Error 1 'source' is already declared as 'Friend WithEvents source in this class".

How do I hook in the override?


VB.NET:
    Protected Overrides Sub source.SetSelectedRowCore(rowIndex As Integer, selected As Boolean)


        If Me.Rows(rowIndex).Cells("Already an Academy" Me).Value = 1 Then
            MsgBox("You can't do that")
            MyBase.SetSelectedRowCore(rowIndex, False)


        Else


            MyBase.SetSelectedRowCore(rowIndex, selected)


        End If


    End Sub

Removing the reference to "source" throws a different error "Error 1 sub 'SetSelectedRowCore' cannot be declared 'Overrides' because it does not override a sub in a base class"
 
You can't override a member of one class in another. SetSelectedRowsCore is a member of the DataGridView class so the only place that it can be overridden is in a class that inherits DataGridView, i.e. in your custom DataGridView class.
 
Back
Top