Solved

datagrid column header sort and highlight

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…

789 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