You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非数据绑定DataGridView列降序排序技术求助(附相关代码)

Hey there! Let's work through how to add descending sorting for your non-data-bound DataGridView columns. I see you're already dynamically setting the sno column values in the RowPostPaint event—great start, we'll make sure sorting plays nice with that too.

Step 1: Set the Column's Sort Mode to Programmatic

First, non-data-bound columns can't use the default automatic sorting, so we need to set their SortMode to Programmatic. You can do this either in the designer (find the column's properties and set SortMode to Programmatic) or in code:

// Replace "YourTargetColumn" with the name of the column you want to sort
dgvDailyEntry.Columns["YourTargetColumn"].SortMode = DataGridViewColumnSortMode.Programmatic;

Step 2: Handle the SortCompare Event for Custom Sorting

Next, we'll use the SortCompare event to define our descending sorting logic. This lets us control exactly how the non-bound column values are compared. Here's an example (adjust the value type to match your column's data):

private void dgvDailyEntry_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
    // Target the column you want to sort
    if (e.Column.Name == "YourTargetColumn")
    {
        // Parse the cell values (change the type if you're working with strings/dates)
        decimal val1 = decimal.Parse(e.CellValue1.ToString());
        decimal val2 = decimal.Parse(e.CellValue2.ToString());
        
        // For descending order, reverse the comparison result
        e.SortResult = val2.CompareTo(val1);
        // Tell the DataGridView we've handled the sorting logic
        e.Handled = true;
    }
}

Step 3: Trigger the Sort (e.g., on Column Header Click)

To let users trigger the sort by clicking the column header, handle the ColumnHeaderMouseClick event. We'll also refresh the grid afterward to update your dynamic sno row numbers:

private void dgvDailyEntry_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    DataGridViewColumn targetColumn = dgvDailyEntry.Columns[e.ColumnIndex];
    if (targetColumn.SortMode == DataGridViewColumnSortMode.Programmatic)
    {
        // Set the sort direction to descending (you can add logic to toggle ascending/descending too)
        ListSortDirection sortDir = ListSortDirection.Descending;
        dgvDailyEntry.Sort(targetColumn, sortDir);
        
        // Refresh to update the sno column values after sorting
        dgvDailyEntry.Refresh();
    }
}

Don't Forget to Bind the Events!

Make sure these events are hooked up to your DataGridView. You can do this in the form's constructor:

public YourFormName()
{
    InitializeComponent();
    // Bind the sorting events
    dgvDailyEntry.SortCompare += dgvDailyEntry_SortCompare;
    dgvDailyEntry.ColumnHeaderMouseClick += dgvDailyEntry_ColumnHeaderMouseClick;
}

Quick Adjustment Tips

  • If your column uses string values instead of numbers, replace the parsing with direct string comparison (e.g., e.CellValue1.ToString().CompareTo(e.CellValue2.ToString()) and reverse it for descending).
  • If you want to toggle between ascending and descending on repeated clicks, track the current sort direction for each column and flip it when the header is clicked.

内容的提问来源于stack exchange,提问作者Dharamraj Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:13:26