Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

datagrid column header sort and highlight

I've been able to sort a datagrid column by clicking on the header. Now, I would like to highlight the column header selected by a user and if possbile also the border of the column. Anyone have an idea?

TIA
0
gabulish
Asked:
gabulish
  • 2
3 Solutions
 
platinum505Commented:
Follow the link

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q792q (5.35 How do I set the width of a column in my DataGrid?)

Instead of setting the width you will need to highlight it.


Kamalakar Nellipudi
0
 
RacinRanCommented:
Download the VB Resource kit here : http://msdn.microsoft.com/vbasic/vbrkit/

It has the Component One datagrid in it.  It has properties for everything including Column border.

Racin
0
 
RonaldBiemansCommented:
Hi gabilish,

I think this should do it (its a bit crude but it seems to work)

create class that inherits datagrid

Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms

    Public Class MyDataGrid
        Inherits DataGrid

        Public Sub New()
            'Warning: Implementation not found
        End Sub

        Private _colclicked As Integer = 0

        Public Property colclicked()
            Get
                Return _colclicked
            End Get
            Set(ByVal Value)
                _colclicked = Value
            End Set
        End Property

        Protected Overloads Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

            Dim hti As DataGrid.HitTestInfo
            hti = Me.HitTest(New Point(e.X, e.Y))
            If (hti.Type = DataGrid.HitTestType.ColumnResize) Then
                Return
            End If
            MyBase.OnMouseMove(e)

        End Sub

        Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim hti As DataGrid.HitTestInfo
            hti = Me.HitTest(New Point(e.X, e.Y))
            If (hti.Type = DataGrid.HitTestType.ColumnHeader) Then
                colclicked = hti.Column
            End If
            MyBase.OnMouseUp(e)
        End Sub
    End Class


then in the form that has the datagrid override the paint event


    Private Sub dataGrid1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)
        Dim x As Rectangle = dataGrid1.GetCellBounds(0, dataGrid1.colclicked)
        x.Y = 22
        x.Height += 1
        CType(dataGrid1.DataSource, DataTable).Columns(dataGrid1.colclicked).ToString()

        If dataGrid1.colclicked >= 0 Then
            e.Graphics.FillRectangle(New SolidBrush(Color.BurlyWood), x)
            e.Graphics.DrawString(CType(dataGrid1.DataSource, DataTable).Columns(dataGrid1.colclicked).ToString(), dataGrid1.Font, New SolidBrush(Color.Black), x.X + 2, x.Y + 1)
        End If

        For xx As Integer = TopRow() To CType(dataGrid1.DataSource, DataTable).Rows.Count
            e.Graphics.DrawRectangle(New Pen(Color.BurlyWood), dataGrid1.GetCellBounds(xx, dataGrid1.colclicked))
        Next
    End Sub

    Public Function TopRow() As Integer

        Dim hti As DataGrid.HitTestInfo
        hti = dataGrid1.HitTest(Me.pointInCell00)
        Return hti.Row

    End Function



don't forget to add a paint handler to the datagrid

like

AddHandler Me.dataGrid1.Paint, New System.Windows.Forms.PaintEventHandler(AddressOf dataGrid1_Paint)
0
 
RonaldBiemansCommented:
sorry in the paint event you can remove this line

CType(dataGrid1.DataSource, DataTable).Columns(dataGrid1.colclicked).ToString()
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now