What events can I trap when a datagrid is sorted by clicking the column header?

Posted on 2004-11-29
Last Modified: 2010-04-24
I have a datagrid that uses a datagridstyle that allows for sorting (by clicking column header).
I made my own textcolumnstyle and added a method that would allow me to resize the dimensions of the cell based on the contents it holds.

My method works fine and the grid is able to display the cells wholly whenever I initialize the grid's contents (updating the data source).

However, once I click on the column header for sorting purposes, the cells in the grid are redrawn to their default (or minimum) width and height, thus cutting the display of cells with rather long texts (it supports multiline display).

I tried calling the method for resizing in the Paint method of the textcolumnstyle.
This initially solves my problem but the user is now unable to resize the dimensions of the cell, which is not good.

So I thought maybe I could call my resizing method in one of the events that happens during sorting.
What I found on the internet were mostly for ASP .NET and seems to be not applicable in my case.

Does anybody have a suggestion for me?

Should I disallow sorting from clicking column header and instead make a combo box that would let the user choose which column to sort? But in this case, how do I sort the grid then? I have a datatable as the data source of the grid.
Question by:DrkLight
    LVL 10

    Expert Comment

    the SortCommand event is also fired in this case..

    with the click event of the datagrid.


    Author Comment

    How do I display the SortCommand event in the code editor?
    It is not on "available" for the grid object I created(at least, it is not in the list of overrideable events).
    Or are you suggesting I put something in the click event of my grid object?
    LVL 10

    Expert Comment

    LVL 10

    Expert Comment


    Author Comment

    Thank you for the link.

    But liike I said in my original post, this seems to be applicable only to ASP.NET.

    Also, the sort event is not available in the list of events for my grid object in the code editor.

    Do I have to declare it myself?
    LVL 25

    Accepted Solution

    Hi DrkLight,

    Have you tried creating your own datagrid and override the mousedown event


    Public Class MyDataGrid
     Inherits DataGrid
    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
    Dim pt As New Point(e.X, e.Y)  
    Dim hti As DataGrid.HitTestInfo = Me.HitTest(pt)

    If hti.Type = HitTestType.ColumnHeader Then
    ' do your stuff here
    End If
    End Sub
    End Class


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    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…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 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

    23 Experts available now in Live!

    Get 1:1 Help Now