自定义排序顺序 - DataGridView [英] Custom sorting order - DataGridView

查看:27
本文介绍了自定义排序顺序 - DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 datagridview 中对其进行排序,而无需将数据填充到 + 之后的 3 个值.
数据类型是字符串,datagridview 列是文本.

Is it possible to sort this in a datagridview without padding the data to 3 values after the +.
The datatype is string and the datagridview column is text.

10:10+01
10:10+100
10:10+110
10:10+10

应该是这样的

 10:10+01
 10:10+10
 10:10+100
 10:10+110

也许将排序模式更改为程序化可能会有所帮助?

Maybe changing the sortingmode to programmatic might help?

任何输入将不胜感激

将数据复制到 dt 然后与数据视图绑定的示例.

Example of the data being copied to dt and then bound with a dataview.

DataTable dtTest = new DataTable();
dtTest.Columns.Add("Column1", typeof(string));
dtTest.Rows.Add("10:11+1");
dtTest.Rows.Add("10:11+101");
dtTest.Rows.Add("10:11+101");
dtTest.Rows.Add("10:11+2");
dtTest.Rows.Add("10:11+200");
dtTest.Rows.Add("10:10+1110");
DataView dvTest = new DataView(dtTest);
dataGridView1.DataSource = dvTest;

示例排序顺序

10:10+1110
10:11+1
10:11+101
10:11+101
10:11+2
10:11+200

推荐答案

自定义排序未绑定的 DataGridview

不确定您的数据,但从字面上看,这将为 未绑定 DataGridView DGV:

首先你需要连接一个 SortCompare 处理程序,可能像这样

First you need to hook up a SortCompare handler, maybe like this

 DGV.SortCompare += new DataGridViewSortCompareEventHandler(  this.DGV_SortCompare);

如有必要,您可以在您的列中调用它(或让标题单击完成工作):

If necessary you can call it on your column (or let the Header click do the job):

 DGV.Sort(DGV.Columns[yourColumn], ListSortDirection.Ascending);

这是 SortCompare 事件代码.它使用简单的字符串操作通过用零填充最后一部分来创建可排序版本.

This is the SortCompare event code. It uses simple string manipulation to create a sortable version by padding the last part with zeroes.

 private void DGV_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
 {
   string s1 = e.CellValue1.ToString().Substring(0, 6) + 
               e.CellValue1.ToString().Substring(6).PadLeft(5, '0');
   string s2 = e.CellValue2.ToString().Substring(0, 6) + 
               e.CellValue2.ToString().Substring(6).PadLeft(5, '0');
   e.SortResult = s1.CompareTo(s2);
   e.Handled = true;
 }

对 DGV 排序的三种方法进行了全面的讨论 此处在 MSDN 上. - 显然,这是解决您问题的最简单方法.也相当灵活:您也可以使用 e.columnIndex 参数为其他列创建单独的比较字符串..

There is a comprehensive discussion of three methods to sort a DGV here on MSDN. - Clearly this is the easiest one for your problem. Also rather flexible: You can use the e.columnIndex parameter to create spearate comparison strings for other columns as well..

如果其他列不需要特殊的排序代码,您应该在SortCompare的开头插入这一行:

If other columns need no special sorting code you should insert this line to the beginning of the SortCompare:

  if (e.Column.Index != yourColumn) return;

自定义排序数据绑定 DataGridView

更新:由于您已将问题更改为DataBound DGV,因此对于这种情况,这里有一个类似的解决方案:

Custom sorting a data bound DataGridView

Update: Since you have changed your question to a DataBound DGV, here is a similar solution for this case:

BindingSource BS = new BindingSource();

private void sortButton_Click(object sender, EventArgs e)
{
    DT.Columns.Add("TempSort");
    foreach (DataRow row in DT.Rows)
    {
        string val = row[yourcolumn].ToString();
        row["TempSort"] = val.ToString().Substring(0, 6) + 
                          val.ToString().Substring(6).PadLeft(5, '0');
    }
    BS.DataSource = DT;
    BS.Sort = "TempSort ASC";
    DT.Columns.Remove("TempSort");
    DGV.DataSource = BS;
}

此解决方案假定您的 DataSource 是一个 DataTable DT,并将创建一个名为TempSort"的临时列,并用准备好的数据值版本填充它;它将升序排序.

This solution assumes your DataSource is a DataTable DT and will create a temporary column called "TempSort"`and fills it with the prepared version of the data values; it will sort ascending.

对于排序,我们使用 BindingSource.

For the sorting we use a BindingSource.

要动态控制右列(此处称为yourcolumn")以及排序顺序,您必须自己编写一些代码,以响应ColumnHeaderClick...

To control the right column (here called 'yourcolumn') dynamically as well as the sort order, you will have to write some code yourself, responding to the ColumnHeaderClick...

这篇关于自定义排序顺序 - DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆