Home All Groups Group Topic Archive Search About

Datagrid Multiple Select

Author
3 May 2007 11:08 AM
Andy Baker
I have a VB.NET 2003 application that uses several datagrids. The behaviour
I want from the datagrid is to select (highlight) the entire row when I
click on any cell in a row. I have done this using code from syncfuson.com
to create a class that inherits from DataGridTextBoxColumn and overrides the
its Edit method. i.e.

Public Class DataGridNoActiveCellColumn
    Inherits DataGridTexBoxColumn

    Private SelectedRow as Integer

    Protected Overloads Overrides Sub Edit (ByVal source As CurrencyManager,
ByVal rowNum as Integer...)
        SelectedRow = rowNum
        If (SelectedRow > -1) and (SelectedRow < source.List.Count) then
            Me.DataGridTableStyle.DataGrid.Unselect(SelectedRow)
            Me.DataGridTableStyle.DataGrid.Select(SelectedRow)
        End if
   End Sub
End Class

This works well. For certain grids, I want the user to be able to select
more than one row, and each selected row to be highlighted. To attempt to do
this I created another class similar to the above, but without the
Me.DataGridTableStyle.DataGrid.Unselect(SelectedRow) line, and created my
DataGridTableStyle using that, but it didn't work. How can I highlight
multiple rows at the same time in a .NET datagrid? Thanks in advance.

Andy Baker

Author
3 May 2007 3:03 PM
ClayB
The KB where the above code came from (with a slightly different order
of statement - need to unselect the old one before you assign the row
number) has a solution to also handle multiple selections.

http://www.syncfusion.com/faq/windowsforms/search/856.aspx

======================
Clay Burch
Syncfusion, Inc.
Author
4 May 2007 10:18 AM
Andy Baker
Hello Clay

    Thanks for that. I think I understand the theory, but unfortunately I
cannot download the sample project from that address - I get a 404 error,
file not found. I have created a class DataGridMultipleSelectColumn that
inherits from DataGridTextBoxColumn, that consists of a single sub:
    Protected Overloads Overrides Sub Edit(ByVal source as
CurrencyManager...)
    End Sub
with an empty method. This prevents the clicked cell from being selected, as
I would expect. I have then created a class inheriting from
Windows.Forms.Datagrid and added a boolean property AllowMultipleSelection.
I then override the OnMouseDown Event as follows:
    Protected Overloads Overrides Sub OnMouseDown(ByVal e as MouseEventArgs)
        Dim aHitTestInfo as DataGrid.HitTestInfo
        aHitTestInfo = Me.HitTest(New Point (e.X, e.Y))
        If (aHitTestInfo.Type = Me.HitTestType.Cell and
AllowMultipleSelection) then
            MyBase.OnRowHeaderClick(e)
        End if
    End Sub
However, this doesn't select the row. Is it because OnMouseDown takes an
argument of type MouseEventArgs and OnRowHeaderClick takes an argument of
EventArgs, and if so how do I convert between them? Thanks for your help.

Andy Baker

Show quoteHide quote
"ClayB" <cl***@syncfusion.com> wrote in message
news:1178204623.414154.140030@h2g2000hsg.googlegroups.com...
> The KB where the above code came from (with a slightly different order
> of statement - need to unselect the old one before you assign the row
> number) has a solution to also handle multiple selections.
>
> http://www.syncfusion.com/faq/windowsforms/search/856.aspx
>
> ======================
> Clay Burch
> Syncfusion, Inc.
>