using System.Reflection to invoke private method ColumnHeaderClick

G

Guest

Hi again,
I'm trying to programatically sort a datagrid. I did find the following
code, and it does work, but, when a column header is clicked, the data only
sorts in descending order. Clicking the same column header again doesn't
change anything, although clicking a different column header re-sorts the
data in descending order according to whatever's in that column. I'm pretty
much at a loss as to how to even start fixing this. Any suggestions?
Thanks,
Mel



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

I've discovered that it actually does sort in ascending order, but then
another sort immediately follows to make it look like it's only sorts by
descending. The ascending table could be seen when I inserted a messagebox
in the dgMouse (MouseUp) event. Before ok is pressed, the table ascends.
Then after ok is pressed, it goes to descending. The only change made was
adding the MessageBox. The public void SortColumn(int columnIndex) remains
the same (way below).
Thanks again for any help,
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)
{
if(HitInfo.Type != DataGrid.HitTestType.ColumnHeader)
{
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());
}
 

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

Similar Threads


Top