在C#中排序数据表中的行按长度 [英] Sort DataTable rows by length in c#
问题描述
如何做到以下情形: 我有一些数据表,其中包含例如一些行:
How to do following scenario: I have some DataTable which contains for example some rows:
1.rowa
2.rowab
3.row
4.rowaba
...
ñ。 rowabba
1.rowa
2.rowab
3.row
4.rowaba
...
n. rowabba
如何通过lenght排序行,叫不上名字。我婉通过字段长度排序表。
How to sort rows by lenght, not by name. I wan to to sort Table by length of fields.
推荐答案
您可以添加一个额外的列到你的数据表
,提供包含通话的前pression到 len个()
的功能,从而导致该列的值进行自动计算:
You could add an extra column to your DataTable
, supplying an expression containing a call to the len()
function, causing the column's values to be automatically computed:
table.Columns.Add("LengthOfName", typeof(int), "len(Name)");
那么你可以简单排序在你之前,结合新列的数据表
来您打算使用任何类型的UI控件的网格:
Then you can simply sort on your new column before binding the DataTable
to a grid to whatever kind of UI control you plan to use:
table.DefaultView.Sort = "LengthOfName";
这篇关于在C#中排序数据表中的行按长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!