We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to catch datagrid's sort event?(Click one ColumnHeader)

How to catch datagrid's sort event?(Click one ColumnHeader)

8 Replies

AD Administrator Syncfusion Team October 20, 2002 08:12 PM UTC

You can catch the click on a columnheader and avoid sorting by deriving DataGrid and overriding OnMouseUp.
public class MyDataGrid: DataGrid
{
	protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
	{
		Point p1 = new Point(e.X, e.Y);
		DataGrid.HitTestInfo hti = this.HitTest(p1);
		if(hti.Type != HitTestType.ColumnHeader)
			base.OnMouseUp(e);
	}
}


NI Nicolas November 7, 2002 10:42 AM UTC

This works perfectly but it occurs before the sort event, and then doesn't enable to catch the selected value after the sort event. Is it an other way to proceed ? Nico


TH Thomas November 7, 2002 07:34 PM UTC

Are you trying to grab the sort events return value or are you trying to grab the value of a highlighted element in the grid?


NI Nicolas November 8, 2002 05:57 AM UTC

I'm trying to grab the value of a highlighted element in the grid. The problem is that the click event occurs before the sort event, and that the sort does not generate a currentcell_changed event. I'have look at some MS examples "vb.net 101 samples - VB.NET - Data Access - Build a Master-Detail Windows Form' and when i sort a datagrid the rest of the form is not anymore correctly binded. So i think that the only way to proceed is to set the datagrid property sort to false, and to catch the click event on the column header, and then to manually sort the grid.


TH Thomas November 8, 2002 01:58 PM UTC

Actually, there's an FAQ article on this site that deals with this issue. Try this link: http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q934q That should take you to the article. Hope that helps.


TH Thomas November 8, 2002 01:58 PM UTC

Actually, there's a FAQ article on this site that deals with this issue. Try this link: http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q934q That should take you to the article. Hope that helps.


JI jim February 2, 2003 10:59 PM UTC

If it's bound it's easy: take the View's ListChanged event.... You get a "reset" event in the ViewList changed event. This is the best way to catch sorting if you have a bound grid.... AddHandler dtMaster.DefaultView.ListChanged, AddressOf HandleMasterViewListChange Protected Overrides Sub HandleMasterViewListChange(ByVal sender As Object, ByVal args As System.ComponentModel.ListChangedEventArgs) 'This is a great event: You get a "RESET" when you sort, or even delete. 'Too bad it doesn't fire when you move between rows! If args.ListChangedType = System.ComponentModel.ListChangedType.Reset _ Or args.ListChangedType = System.ComponentModel.ListChangedType.ItemDeleted Then 'Need to refresh detail grid FilterDetailGrid(GetCurrentMasterRow) End If End Sub


SR SrimuruganG January 24, 2004 07:39 AM UTC

Insteed of these things Please paste the following datagrid Class and Follow the Steps, which is given below the code. Public Class FSDataGrid Inherits System.Windows.Forms.DataGrid Protected Overloads Overrides Sub OnMouseUp(ByVal e As MouseEventArgs) If (Me.HitTest(e.X, e.Y).Type <> DataGrid.HitTestType.ColumnHeader) Then MyBase.OnMouseUp(e) Else MyBase.OnMouseUp(e) MyBase.OnCurrentCellChanged(e) End If End Sub End Class Steps to Follow : 1. Paste the above code inside the Namespace. 2. Goto #Region " Windows Form Designer generated code " there you can find the Declaration of the datagrid. 3. Insteed of System.Windows.Forms.Datagrid give the Name of the Class (Namespace).FSDatagrid. Declare Like "Friend WithEvents DataGrid1 As Test.FSDataGrid" where - Test is Namespace. 4. Change it in the Sub InitializeComponent(). 5. I hope it will work fine.

Loader.
Live Chat Icon For mobile
Up arrow icon