Sorting a datagrid on a hidden column

K

Kirti

Hi,
I would like to sort a datagrid not on the column that is displayed,
but on another hidden column. I have AllowSorting = true.


This is what I am doing in load.
//tmpMembList is my DataSet that has the required results. The column
that is displayed is AccountNumber but I would like to sort on
'SortColumn' which is a hidden column in the datagrid. This seems to
work fine and when I get the form loaded, i can see the grid sorted.

Load()
{
// Get the DataTable associated with this datagrid.
dataTable = tmpMembList.Tables[0];

// Get the DataView associated with the DataTable.
dataView = dataTable.DefaultView;

dataView.ApplyDefaultSort = false;

dataView.Sort = "[SortColumn]";

dgMemberList.DataSource = dataTable;

if (dgMemberList.VisibleRowCount >0)
dgMemberList.Select(0);
}

Now I want to sort when the user clicks on the column header.
This is what I have been trying. I am capturing the mouse down event
and then getting the dataview and changing the sort again


private void dgMemberList_MouseDown(object o, MouseEventArgs mea)
{
string columnName;
DataGrid.HitTestInfo hitTest;

// Use only left mouse button clicks.
if (mea.Button == MouseButtons.Left)
{
// Perform a hit test to determine where the mousedown event
occured.
hitTest = dgMemberList.HitTest(mea.X, mea.Y);

// If the mousedown event occured on a column header,
// then perform the sorting operation.

if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
{
// Reset mouse button state.
buttonPress = true;

// Get the DataTable associated with this datagrid.
dataTable = (DataTable)dgMemberList.DataSource;

// Get the DataView associated with the DataTable.
dataView = dataTable.DefaultView;
dataView.ApplyDefaultSort = false;

// Get the name of the column that was clicked.
columnName = dataTable.Columns[hitTest.Column].ColumnName;

// If the sort property of the DataView is already the current
// column name, sort that column in descending order.
// Otherwise, sort on the column name.
if (columnName == "AccountNumber")
{
if (dataView.Sort == "[SortColumn]")
dataView.Sort = "[SortColumn] DESC";
else
dataView.Sort = "[SortColumn]";
}
}
}
}

I can see that it is sorting in the right way when I do the mouse
down. But when I leave the mouse (mouse up) it again sorts it on the
accountnumber.

if i put this code in Mouse up, it does not work at all.

Please HELP.

Thanks,
Kirti
 
J

Jeremy Todd

Kirti said:
Hi,
I would like to sort a datagrid not on the column that is displayed,
but on another hidden column. I have AllowSorting = true.
I can see that it is sorting in the right way when I do the mouse
down. But when I leave the mouse (mouse up) it again sorts it on the
accountnumber.

I'm just guessing here, but I think what's happening is that your
sorting routine is working on MouseDown, but then the DataGrid's built-in
sorting is kicking in on MouseUp. Since you're handling sorting manually,
try changing AllowSorting to false and see if that solves the problem.

Jeremy
 
E

EMW

if you do the sorting inside the eventhandler of mousedown it is likely the
sorting is disposed when the eventhandler procedure ends.
 

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