Solved

datagrid column header sort and highlight

Posted on 2004-10-05
7
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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