C# DataGridView排序

C# DataGridView排序  

[转]http://hi.baidu.com/%B9%FE%B9%FE%D6%F1%CB%F1/blog/item/bbff9ac3ad9ca63ce4dd3bdc.html

private void Button1_Click(object sender, EventArgs e)
{
    if (DataGridView1.CurrentCell == null)
        return;

    //设定排序的列
     DataGridViewColumn sortColumn = DataGridView1.CurrentCell.OwningColumn;

    //设定排序的方向(升序、降序)
     ListSortDirection sortDirection = ListSortDirection.Ascending;
    if (DataGridView1.SortedColumn != null &&
         DataGridView1.SortedColumn.Equals(sortColumn))
     {
         sortDirection =
             DataGridView1.SortOrder == SortOrder.Ascending ?
             ListSortDirection.Descending : ListSortDirection.Ascending;
     }

    //进行排序
     DataGridView1.Sort(sortColumn, sortDirection);
}

你可能感兴趣的:(C# DataGridView排序)