Solved

datagrid column header sort and highlight

Posted on 2004-10-05
7
387 Views
Last Modified: 2008-01-09
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
Comment
Question by:gabulish
  • 2
7 Comments
 
LVL 2

Accepted Solution

by:
platinum505 earned 134 total points
ID: 12229390
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
 
LVL 3

Assisted Solution

by:RacinRan
RacinRan earned 133 total points
ID: 12229394
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
 
LVL 25

Assisted Solution

by:RonaldBiemans
RonaldBiemans earned 133 total points
ID: 12232110
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
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12232152
sorry in the paint event you can remove this line

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now