DataGridView 列自定义排序

fencole 2007-05-25 09:37:08
如何按照用户自己设的列顺序排?假设用户想要的顺序已知
...全文
2443 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdl2005lyx 2007-05-25
  • 打赏
  • 举报
回复
lz:其实这两种方法本质是一样的,我们通常的操作肯定是对一列的数据进行某种排序,你想想,列排序离开数据,肯定没有实际意义!
sdl2005lyx 2007-05-25
  • 打赏
  • 举报
回复
那用DataGridView.Sort 方法 (DataGridViewColumn, ListSortDirection) :
此方法通过比较指定列中的值来对 DataGridView 的内容进行排序。默认情况下,排序操作使用 Compare 方法并通过 DataGridViewCell.Value 属性来比较列中的单元格对。

对于 SortMode 属性设置为 DataGridViewColumnSortMode.Automatic 的列,会自动设置 SortedColumn 和 SortOrder 属性并显示相应的排序标志符号。对于 SortMode 属性设置为 DataGridViewColumnSortMode.Programmatic 的列,必须由您自己通过 DataGridViewColumnHeaderCell.SortGlyphDirection 属性来显示排序标志符号。

可以通过处理 SortCompare 事件来对此方法使用的排序操作进行自定义。只有当 DataSource 属性尚未设置时,才会发生此事件。

当 DataSource 属性已设置时,此方法仅适用于数据绑定列。数据绑定列设置自己的 DataGridViewColumn.DataPropertyName 属性。这会导致 DataGridViewColumn.IsDataBound 属性返回 true。

如果 DataGridView 控件同时包含绑定列和未绑定列,则您必须实现虚拟模式,以便在按绑定列对控件进行排序时保持未绑定列的值。这可以通过将 VirtualMode 属性设置为 true 并处理 CellValueNeeded 事件来完成。如果该列是可编辑的,还应当处理 CellValuePushed 事件。

代码实例:
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;
}
}
fencole 2007-05-25
  • 打赏
  • 举报
回复
sdl2005lyx() :我是对列排序,不是对数据排序
sdl2005lyx 2007-05-25
  • 打赏
  • 举报
回复
DataGridView.Sort 方法 (IComparer):
使用此方法可以对 DataGridView 类的排序功能进行高级自定义。为了实现高度自定义的排序操作,可以为 ColumnHeaderMouseClick 事件编写事件处理程序,并使用某个类的实例调用此方法,该类将 System.Collections.IComparer 接口作为一个参数实现。在这种情况下,通常将 DataGridViewColumn.SortMode 属性设置为 DataGridViewColumnSortMode.Programmatic,以禁用自动排序并为排序标志符号留出空间。在将按列进行的排序设置为编程排序模式时,您必须通过设置 DataGridViewColumnHeaderCell.SortGlyphDirection 属性来自己显示排序标志符号。

只有当未设置 DataSource 属性时,此方法才有效。当您将 DataGridView 控件绑定到外部数据源时,必须使用该数据源提供的排序操作。当您通过实现虚拟模式来提供自己的数据源时,也必须自己处理排序操作。

自动调用此方法会将 CurrentCell 属性设置为空引用

代码实例:
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;
}
}
fencole 2007-05-25
  • 打赏
  • 举报
回复
我自己试过,假设一个DataGridView有5列,然后希望将这5列倒序,分别将原来的列的DisplayIndex颠倒,DataGridView居然会乱序
周公 2007-05-25
  • 打赏
  • 举报
回复
DatagridView的没这么做过,以前DataTable做过。
我当时的做法是定义好要显示那些列,并且定义好显示顺序,再按照那个设定的去做。
fencole 2007-05-25
  • 打赏
  • 举报
回复
up

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧