Solved

datagrid column header sort and highlight

Posted on 2004-10-05
7
390 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 55
Get String split 5 47
Protecting vb6 & .Net code Obfuscation 18 94
Show/Hide ASP.NET controls with JQuery best practices 3 14
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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