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
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