C#中DataGridView点击列头排序问题 我的这段代码怎么就不能实现“点击dgv列头就换升降序”排呢 请大侠指教
privatevoiddgvSalaryForPerformance_ColumnHeaderMouseClick(objectsender,DataGridViewCe...
private void dgvSalaryForPerformance_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (_columnIndex == e.ColumnIndex)
{
if (_sortDirection == 0)
{
_sortDirection = 1;
}
else
{
_sortDirection = 0;
}
}
else
{
_columnIndex = e.ColumnIndex; //记录列索引
_sortDirection = 0; //设为升序标记
}
//重新给Dgv排序
RefreshDataGridView(this.dgvSalaryForPerformance, _sortDirection);
}
#endregion
#region 刷新dgv数据源 以重新排序
private void RefreshDataGridView(DataGridView dgv, int sortdirection)
{
if (dgv.DataSource == null)
{
return;
}
//DataGridViewColumn sortColumn = this.dgvSalaryForPerformance.Columns[_columnIndex];
DataGridViewColumn sortColumn = dgv.SortedColumn;
CurrencyManager cmdgv = (CurrencyManager)this.BindingContext[dgv.DataSource];
if (cmdgv != null)
{
cmdgv.Refresh(); //刷新数据源
}
if (sortColumn != null)
{
if (sortdirection == 0)
{
ListSortDirection sortDirection = ListSortDirection.Ascending;
dgv.Sort(sortColumn, sortDirection);
}
if (sortdirection == 1)
{
ListSortDirection sortDirection = ListSortDirection.Descending;
dgv.Sort(sortColumn, sortDirection);
}
}
} 展开
{
if (_columnIndex == e.ColumnIndex)
{
if (_sortDirection == 0)
{
_sortDirection = 1;
}
else
{
_sortDirection = 0;
}
}
else
{
_columnIndex = e.ColumnIndex; //记录列索引
_sortDirection = 0; //设为升序标记
}
//重新给Dgv排序
RefreshDataGridView(this.dgvSalaryForPerformance, _sortDirection);
}
#endregion
#region 刷新dgv数据源 以重新排序
private void RefreshDataGridView(DataGridView dgv, int sortdirection)
{
if (dgv.DataSource == null)
{
return;
}
//DataGridViewColumn sortColumn = this.dgvSalaryForPerformance.Columns[_columnIndex];
DataGridViewColumn sortColumn = dgv.SortedColumn;
CurrencyManager cmdgv = (CurrencyManager)this.BindingContext[dgv.DataSource];
if (cmdgv != null)
{
cmdgv.Refresh(); //刷新数据源
}
if (sortColumn != null)
{
if (sortdirection == 0)
{
ListSortDirection sortDirection = ListSortDirection.Ascending;
dgv.Sort(sortColumn, sortDirection);
}
if (sortdirection == 1)
{
ListSortDirection sortDirection = ListSortDirection.Descending;
dgv.Sort(sortColumn, sortDirection);
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询