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

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

问题描述

是否可以在datagridview中对它进行排序,而不必在+后面填充数据到3个值。

数据类型为字符串,而datagridview列为text。

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的排序



不确定您的数据,但是按字面意义将其用于 unbound DataGridView DGV

首先,您需要连接 SortCompare 处理程序,也许是这样

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

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

如有必要,可以在列上调用它(或让Header单击以完成工作):

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

要动态控制右列(此处称为 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天全站免登陆