datagridview排序
时间: 2023-07-06 07:29:53
浏览: 119
DataGridView可以通过设置它的排序[属性](https://geek.csdn.net/educolumn/5280ca4eda584c6baf7c2ec8010927f9?spm=1055.2569.3001.10083)来实现排序。可以在DataGridView的ColumnHeaderMouseClick事件中实现排序,例如:
```csharp
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
// 获取当前点击的列
DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
// 判断当前列是否已经是排序列
if (column.SortMode != DataGridViewColumnSortMode.Programm[ati](https://geek.csdn.net/educolumn/150d7073277950db5f09620704e791cf?spm=1055.2569.3001.10083)c || column.HeaderCell.SortGlyphDirection == Sort[ord](https://geek.csdn.net/educolumn/514d2828312f543fa61b516e089a3f41?spm=1055.2569.3001.10083)er.None)
// 如果不是排序列或者当前列没有排序标志,则按升序排序
dataGridView1.Sort(column, ListSortDirection.Ascending);
column.HeaderCell.SortGlyphDirection = Sort[ord](https://geek.csdn.net/educolumn/514d2828312f543fa61b516e089a3f41?spm=1055.2569.3001.10083)er.Ascending;
else if (column.HeaderCell.SortGlyphDirection == Sort[ord](https://geek.csdn.net/educolumn/514d2828312f543fa61b516e089a3f41?spm=1055.2569.3001.10083)er.Ascending)
// 如果
```