datagrid sorting occuring twice?

G

Guest

Hi,
I'm programatically sorting in a datagrid. When a column header is clicked,
the sort happens twice for some reason, making it looks like it only sorts in
descending order. I can tell it sorts twice because I inserted a messagebox
in the dgMouse (MouseUp) event. Before ok is pressed, the table changes
from the order it was loaded to ascending order.
After ok is pressed, it goes to descending. The code is below. Any idea
why sorting happens twice?
Thanks!
Mel

private void dgMouse(object sender, MouseEventArgs e)
{
DataGrid.HitTestInfo HitInfo = dg.HitTest(e.X, e.Y);
if (HitInfo.Row < tCat.Rows.Count && HitInfo.Row > -1)
{
this.dg.Select(HitInfo.Row);
row = HitInfo.Row;
xFile = int.Parse((tCat.Rows[row]["File"]).ToString());
}
if(HitInfo.Type == DataGrid.HitTestType.ColumnHeader)
{
columnIndex = HitInfo.Column;
this.SortColumn(columnIndex);
MessageBox.Show(xFile.ToString());
}

public void SortColumn(int columnIndex)
{
System.ComponentModel.PropertyDescriptor pd =
dg.TableStyles[0].GridColumnStyles[columnIndex].PropertyDescriptor;

//now invoke ColumnHeaderClicked method using system.reflection tools
System.Reflection.MethodInfo mi =
typeof(System.Windows.Forms.DataGrid).GetMethod("ColumnHeaderClicked",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
mi.Invoke(dg, new object[] {pd});
}
 
G

Guest

Ok, that was definitely it (thanks!). But then how can I tell when the
sorting is over so that I can reselect the row that was selected before the
sort?


James Curran said:
A WinForms DataGrid should sort-on-column-click automatically. Remove
your event handler entirely, and try it.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

melanieab said:
Hi,
I'm programatically sorting in a datagrid. When a column header is clicked,
the sort happens twice for some reason, making it looks like it only sorts in
descending order. I can tell it sorts twice because I inserted a messagebox
in the dgMouse (MouseUp) event. Before ok is pressed, the table changes
from the order it was loaded to ascending order.
After ok is pressed, it goes to descending. The code is below. Any idea
why sorting happens twice?
Thanks!
Mel

private void dgMouse(object sender, MouseEventArgs e)
{
DataGrid.HitTestInfo HitInfo = dg.HitTest(e.X, e.Y);
if (HitInfo.Row < tCat.Rows.Count && HitInfo.Row > -1)
{
this.dg.Select(HitInfo.Row);
row = HitInfo.Row;
xFile = int.Parse((tCat.Rows[row]["File"]).ToString());
}
if(HitInfo.Type == DataGrid.HitTestType.ColumnHeader)
{
columnIndex = HitInfo.Column;
this.SortColumn(columnIndex);
MessageBox.Show(xFile.ToString());
}

public void SortColumn(int columnIndex)
{
System.ComponentModel.PropertyDescriptor pd =
dg.TableStyles[0].GridColumnStyles[columnIndex].PropertyDescriptor;

//now invoke ColumnHeaderClicked method using system.reflection tools
System.Reflection.MethodInfo mi =
typeof(System.Windows.Forms.DataGrid).GetMethod("ColumnHeaderClicked",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
mi.Invoke(dg, new object[] {pd});
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top