In. net, the DataGrid supports sorting, but does not support bidirectional sorting. I used it and read some related posts. I tried a method and it worked, mainly using the DataGrid. attributes stores a parameter and modifies the sortexpression of the datagridcolumn in onsortcommand.CodeAs follows:
Private void binddata ()
{
Datatable dt = .......;
If (DT! = NULL)
{
Dataview DV = DT. defaultview;
If (maid ["sortby"]! = NULL)
{
DV. Sort = maid ["sortby"];
}
Datagrid1.datasource = DV;
Datagrid1.databind ();
}
}
Private void datagridsort (Object source, system. Web. UI. webcontrols. datagridsortcommandeventargs E)
{
Datagrid1.attributes ["sortby"] = sortstr;
This. binddata ();
// Locate the sorted column and modify its sorting attribute
Datagridcolumn CLM = NULL;
For (INT I = 0; I <datagrid1.columns. Count; I ++)
{
If (maid [I]. sortexpression = E. sortexpression)
{
CLM = maid [I];
Break;
}
}
If (CLM = NULL) return;
If (E. sortexpression. tolower (). indexof ("DESC")> 0)
{
CLM. sortexpression = E. sortexpression. tolower (). Replace ("DESC", "ASC ");
}
Else
{
If (E. sortexpression. tolower (). indexof ("ASC")> 0)
{
CLM. sortexpression = E. sortexpression. tolower (). Replace ("ASC", "DESC ");
}
Else
{
CLM. sortexpression = E. sortexpression. tolower () + "DESC ";
}
}
}