[C#] //窗体的Load事件处理器 private void Form1_Load(object sender, EventArgs e) { //自动排序 foreach (DataGridViewColumn c in DataGridView1.Columns) c.SortMode = DataGridViewColumnSortMode.Automatic; } // Button1的Click事件处理器 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); }