By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,182 Members | 1,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,182 IT Pros & Developers. It's quick & easy.

datagrid sorting occuring twice?

P: n/a
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("C olumnHeaderClicked",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
mi.Invoke(dg, new object[] {pd});
}
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <me*******@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
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("C olumnHeaderClicked",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
mi.Invoke(dg, new object[] {pd});
}

Nov 17 '05 #2

P: n/a
mdb
=?Utf-8?B?bWVsYW5pZWFi?= <me*******@discussions.microsoft.com> wrote in
news:B6**********************************@microsof t.com:
Any idea
why sorting happens twice?


Just a thought, but is there any chance you are subscribing to the MouseUp
event twice?

--
-mdb
Nov 17 '05 #3

P: n/a
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" wrote:
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" <me*******@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
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("C olumnHeaderClicked",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
mi.Invoke(dg, new object[] {pd});
}


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.