Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello.

 

My data grid is bound to a datasource and it's working great except for one thing.

 

When I select on a particular row on the datagrid the cell I select becomes highlighted and not the entire row.

 

I don't want just the one cell highlighted. I want to highlight the entire row.

 

How do I do this?

 

Thanks.

Posted

Thanks for the response. That worked almost like I want.

 

Yes the entire row is highlighted. However; the cell I selected is highlighted with a different color/selection format. I don't want this. Is there anyway to turn off the cell selection?

  • 3 months later...
Posted

where dgaset is the name of your data grid

 

Private Sub DGASET_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGASET.MouseUp

Dim pt = New Point(e.X, e.Y)

 

Dim hti As DataGrid.HitTestInfo = DGASET.HitTest(pt)

 

If hti.Type = DataGrid.HitTestType.Cell Then

 

DGASET.CurrentCell = New DataGridCell(hti.Row, hti.Column)

 

DGASET.Select(hti.Row)

 

End If

End Sub

  • 5 months later...
Posted

OK, this works for a mouse click, but what if you wnat the same behaviour with the user using the arrow keys. It works holding the shift key but not with simple arrow key navigation...

 

/Rickard

  • 11 months later...
Posted

Since I wanted many of the rows in the grid to be color-coded based on the value of a particular column per row, I created a new class that inherits the DataGridTextBoxColumn class. So, when defining the grid's default table style, I instantiated the column styles for that table style using the new class.

 

Inside of the new class, I created an overrided sub for the Paint event of the grid, as well as an overrided sub for the Edit event, which will handle the cell not being selected as you move from row to row, regardless of whether or not a mouse-click does it or navigation with the keyboard arrows.

 

Below is code kind of explaining this a bit better...

Here's the code for the class, including the Paint and Edit overrided events

Option Explicit On 
Option Strict On

Public Class clsGridColumnMgr
   Inherits DataGridTextBoxColumn

   Private SelectedRow As Integer = -1
   Private ds as Dataset

   Public Sub New()
       ' Default constructor for the class
       ds = ObtainColumnSettings()
   End Sub

   Protected Overloads Overrides Sub Paint(ByVal graph As Graphics, _
        ByVal rectbounds As Rectangle, ByVal curmngrSrc As _
        CurrencyManager, ByVal RowNumber As Integer, ByVal _
        ForeColorBrush As Brush, ByVal BackColorBrush As Brush, _
        ByVal AlignmentRight As Boolean)
       ' This routine overrides the Paint event for the grid; thus, we will set the cell color
       ' based on the value of column 0
       ' Grab the cell contents as an object
       Dim ObjVal As Object
       ObjVal = Me.GetColumnValueAtRow(curmngrSrc, RowNumber)

       Try
           If Not (IsNothing(ObjVal) Or IsDBNull(ObjVal)) Then
               Dim j As Integer, k As Integer
               Dim red As Integer, green As Integer, blue As Integer, intThisItem As Integer

               ' Determine the value of column 0 for this row
               Dim grid As DataGrid = Me.DataGridTableStyle.DataGrid
               Dim intValue As Integer = CInt(grid.Item(RowNumber, 0).ToString)

               ' Determine the rowcolor for this column value
               ' (strBasic is returned as: "1,37,103", with each value being the red/green/blue color RGB value)
               Dim strBasic As String
               If grid.CurrentRowIndex = RowNumber Then
                   strBasic = RetrieveRowColor(intValue)
               Else
                   strBasic = RetrieveRowColor(intValue)
               End If
               strBasic = strBasic & ","

               ' Retrieve the red, green, and blue color attributes for the column value
               Dim intStart As Integer = 0
               Dim intEnd As Integer = InStr(1, strBasic, ",")
               k = 0
               Do While intEnd > 0
                   k = k + 1
                   intThisItem = CInt(Mid(strBasic, intStart + 1, intEnd - intStart))

                   Select Case k
                       Case 1 : red = intThisItem
                       Case 2 : green = intThisItem
                       Case 3 : blue = intThisItem
                   End Select
                   intStart = intEnd
                   intEnd = InStr(intStart + 1, strBasic, ",")
               Loop

               ' Color the contents of the cell appropriately
               Dim mybrush As New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(red, green, blue))
               BackColorBrush = mybrush
               If grid.CurrentRowIndex = RowNumber Then
                   ForeColorBrush = Brushes.White
               Else
                   ForeColorBrush = Brushes.Black
               End If
           End If
       Catch ex As Exception
           ' Empty catch
       Finally
           ' Call Paint from the base class to 
           ' accomplish the actual drawing.
           MyBase.Paint(graph, rectbounds, curmngrSrc, RowNumber, _
               BackColorBrush, ForeColorBrush, AlignmentRight)

           ' Dispose of objects no longer used
           ObjVal = Nothing
       End Try

   End Sub

   Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, _
       ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, _
       ByVal rOnly As Boolean, ByVal instantText As String, _
       ByVal cellIsVisible As Boolean)

       ' This routine overrides the Edit event for a selected cell, thus making the cell unselected

       ' Make sure the previous selection is valid
       If SelectedRow > -1 And SelectedRow < source.List.Count + 1 Then
           DataGridTableStyle.DataGrid.UnSelect(SelectedRow)
           SelectedRow = rowNum
           DataGridTableStyle.DataGrid.Select(SelectedRow)
       End If

   End Sub

   Private Function RetrieveRowColor(ByVal intValue As Integer) As String
       ' This routine determines the main cell (and row) color for the specified column value
       Dim j As Integer

       ' Determine which row color to return
       For j = 0 To ds.Tables(0).Rows.Count - 1
           If CInt(ds.Tables(0).Rows(j).Item("column_value").ToString) = intValue Then
               RetrieveRowColor = ds.Tables(0).Rows(j).Item("row_color").ToString
               Exit For
           End If
       Next

       ' Dispose of unused objects
       ds.Dispose()

   End Function
End Class

 

And in the Form_Load event, I use the class to instantiate the default column styles for the default table style:

       ' Instantiate table style and column style objects for the grid
       Dim gridtableStyle As New DataGridTableStyle()
       Dim gridcolumnStyle1 As New clsGridColumnMgr()
       Dim gridcolumnStyle2 As New clsGridColumnMgr()
       Dim gridcolumnStyle3 As New clsGridColumnMgr()
       Dim gridcolumnStyle4 As New clsGridColumnMgr()

       ' Define the column styles for the grid
       gridcolumnStyle1.HeaderText = "Column 1"
       gridcolumnStyle1.MappingName = "DBColumn1"
       gridcolumnStyle1.Width = 50
       gridcolumnStyle2.HeaderText = "Column 2"
       gridcolumnStyle2.MappingName = "DBColumn2"
       gridcolumnStyle2.Width = 260
       gridcolumnStyle3.HeaderText = "Column 3"
       gridcolumnStyle3.MappingName = "DBColumn3"
       gridcolumnStyle3.Width = 260
       gridcolumnStyle4.HeaderText = "Column 4"
       gridcolumnStyle4.MappingName = "DBColumn4"
       gridcolumnStyle4.Width = 260

       ' Add the columns to the table style
       gridtableStyle.GridColumnStyles.Add(gridcolumnStyle1)
       gridtableStyle.GridColumnStyles.Add(gridcolumnStyle2)
       gridtableStyle.GridColumnStyles.Add(gridcolumnStyle3)
       gridtableStyle.GridColumnStyles.Add(gridcolumnStyle4)

       ' Add the table style information to display the info in the grid
       gridtableStyle.RowHeadersVisible = False
       gridtableStyle.HeaderBackColor = Color.Silver
       gridtableStyle.AllowSorting = False
       gridtableStyle.GridLineStyle = DataGridLineStyle.None
       gridThisOne.TableStyles.Add(gridtableStyle)

       ' Setting additional properties of the grid
       gridThisOne.RowHeadersVisible = False
       gridThisOne.ColumnHeadersVisible = True
       gridThisOne.ParentRowsVisible = False
       gridThisOne.CaptionVisible = False

 

Finally, in the grid's MouseUp event, I enable the highlighting of the entire row (during mouse-clicks). (This may actually be handled by the Edit event of the custom class, but I left it here, just in case...):

   Private Sub gridThisOne_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gridThisOne.MouseUp

       ' Highlight the entire row of the selected cell in the grid
       Dim pt As Point = New Point(e.X, e.Y)
       Dim hti As DataGrid.HitTestInfo = gridThisOne.HitTest(pt)
       If hti.Type = DataGrid.HitTestType.Cell Then
           gridThisOne.CurrentCell = New DataGridCell(hti.Row, hti.Column)
           gridThisOne.Select(hti.Row)
       End If

   End Sub

 

Sorry to make this so long. But I figured a little background might make things more clear. Hope all of this insanity helps! :cool:

3,450,897,223 posts away from crazy...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...