自定义排序顺序——DataGridView
Custom sorting order - DataGridView
是否可以在 datagridview 中对此进行排序,而不用将数据填充到 + 后的 3 个值。
数据类型为字符串,datagridview 列为文本。
10:10+01
10:10+100
10:10+110
10:10+10
应该这样排序
10:10+01
10:10+10
10:10+100
10:10+110
也许将排序模式更改为程序化可能会有帮助?
如有任何意见,我们将不胜感激
编辑:数据被复制到 dt 然后与数据视图绑定的示例。
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
处理程序,可能像这样
DGV.SortCompare += new DataGridViewSortCompareEventHandler( this.DGV_SortCompare);
如有必要,您可以在您的专栏中调用它(或让 Header 单击完成):
DGV.Sort(DGV.Columns[yourColumn], ListSortDirection.Ascending);
这是 SortCompare 事件代码。它使用简单的字符串操作通过用零填充最后部分来创建可排序版本。
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 排序的三种方法进行了全面讨论 here on MSDN. - 显然,这是解决您的问题的最简单方法。同样相当灵活:您也可以使用 e.columnIndex
参数为其他列创建单独的比较字符串..
如果其他列不需要特殊的排序代码,您应该将此行插入到 SortCompare
:
的开头
if (e.Column.Index != yourColumn) return;
自定义排序数据绑定 DataGridView
更新: 由于您已将问题更改为 DataBound DGV,这里有一个类似的解决方案:
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" 的临时列,并用准备好的数据值版本填充它;它会升序排序。
对于排序,我们使用 BindingSource
。
要动态控制右列(此处称为“yourcolumn
”)以及排序顺序,您必须自己编写一些代码,以响应 ColumnHeaderClick
...
是否可以在 datagridview 中对此进行排序,而不用将数据填充到 + 后的 3 个值。
数据类型为字符串,datagridview 列为文本。
10:10+01
10:10+100
10:10+110
10:10+10
应该这样排序
10:10+01
10:10+10
10:10+100
10:10+110
也许将排序模式更改为程序化可能会有帮助?
如有任何意见,我们将不胜感激
编辑:数据被复制到 dt 然后与数据视图绑定的示例。
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
处理程序,可能像这样
DGV.SortCompare += new DataGridViewSortCompareEventHandler( this.DGV_SortCompare);
如有必要,您可以在您的专栏中调用它(或让 Header 单击完成):
DGV.Sort(DGV.Columns[yourColumn], ListSortDirection.Ascending);
这是 SortCompare 事件代码。它使用简单的字符串操作通过用零填充最后部分来创建可排序版本。
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 排序的三种方法进行了全面讨论 here on MSDN. - 显然,这是解决您的问题的最简单方法。同样相当灵活:您也可以使用 e.columnIndex
参数为其他列创建单独的比较字符串..
如果其他列不需要特殊的排序代码,您应该将此行插入到 SortCompare
:
if (e.Column.Index != yourColumn) return;
自定义排序数据绑定 DataGridView
更新: 由于您已将问题更改为 DataBound DGV,这里有一个类似的解决方案:
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" 的临时列,并用准备好的数据值版本填充它;它会升序排序。
对于排序,我们使用 BindingSource
。
要动态控制右列(此处称为“yourcolumn
”)以及排序顺序,您必须自己编写一些代码,以响应 ColumnHeaderClick
...