微軟提供幾個範例
一個是 public virtual void Sort(DataGridViewColumn dataGridViewColumn, ListSortDirection direction);
private void sortButton_Click(object sender, System.EventArgs e) { // Check which column is selected, otherwise set NewColumn to null. DataGridViewColumn newColumn = dataGridView1.Columns.GetColumnCount( DataGridViewElementStates.Selected) == 1 ? dataGridView1.SelectedColumns[0] : null; DataGridViewColumn oldColumn = dataGridView1.SortedColumn; ListSortDirection direction; // If oldColumn is null, then the DataGridView is not currently sorted. if (oldColumn != null) { // Sort the same column again, reversing the SortOrder. if (oldColumn == newColumn && dataGridView1.SortOrder == SortOrder.Ascending) { direction = ListSortDirection.Descending; } else { // Sort a new column and remove the old SortGlyph. direction = ListSortDirection.Ascending; oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None; } } else { direction = ListSortDirection.Ascending; } // If no column has been selected, display an error dialog box. if (newColumn == null) { MessageBox.Show("Select a single column and try again.", "Error: Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { dataGridView1.Sort(newColumn, direction); newColumn.HeaderCell.SortGlyphDirection = direction == ListSortDirection.Ascending ? SortOrder.Ascending : SortOrder.Descending; } }
//------------------------------------------------------------------------------
另一個是 public virtual void Sort(IComparer comparer);
private void Button1_Click( object sender, EventArgs e ) { if ( RadioButton1.Checked == true ) { DataGridView1.Sort( new RowComparer( SortOrder.Ascending ) ); } else if ( RadioButton2.Checked == true ) { DataGridView1.Sort( new RowComparer( SortOrder.Descending ) ); } } private class RowComparer : System.Collections.IComparer { private static int sortOrderModifier = 1; public RowComparer(SortOrder sortOrder) { if (sortOrder == SortOrder.Descending) { sortOrderModifier = -1; } else if (sortOrder == SortOrder.Ascending) { sortOrderModifier = 1; } } public int Compare(object x, object y) { DataGridViewRow DataGridViewRow1 = (DataGridViewRow)x; DataGridViewRow DataGridViewRow2 = (DataGridViewRow)y; // Try to sort based on the Last Name column. int CompareResult = System.String.Compare( DataGridViewRow1.Cells[1].Value.ToString(), DataGridViewRow2.Cells[1].Value.ToString()); // If the Last Names are equal, sort based on the First Name. if ( CompareResult == 0 ) { CompareResult = System.String.Compare( DataGridViewRow1.Cells[0].Value.ToString(), DataGridViewRow2.Cells[0].Value.ToString()); } return CompareResult * sortOrderModifier; } }
//------------------------------------------------------------------------------
我用範例修改了一個簡單的
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Columns.Add("Col1","A");
dataGridView1.Columns.Add("Col2","B");
dataGridView1.Rows.Add(1, 5);
dataGridView1.Rows.Add(3, 1);
dataGridView1.Rows.Add(6, 2);
dataGridView1.Rows.Add(5, 6);
dataGridView1.Rows.Add(4, 3);
dataGridView1.Rows.Add(2, 4);
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
}
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Ascending);
}
留言列表