![](/img/trans.png)
[英]How to move a DataTable row to the first position of its DataTable
[英]Change row position in DataTable or move row in DataTable
我有一个必须排序的DataTable,但是即使在对DataTable排序之后,我也想返回特定的行作为最后一行。 我将通过单列中的字符串值来标识此行。
public DataTable StandardReport3B(string missionId, string reportType, string startDate, string endDate)
{
List<long> missionIdList = new List<long>();
string[] missionIds = missionId.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string mission in missionIds)
{
missionIdList.Add(Convert.ToInt64(mission));
}
DataTable dt = new DataTable();
object reportData = null;
using (var ctx = new MedicalServiceEntities())
{
reportData = ctx.GetData(startDate, endDate, missionId).ToList();
}
dt = this.GenericListToDataTable(reportData);
DataView dv = dt.DefaultView;
dv.Sort = dt.Columns[0].ColumnName + " Asc";
return dv.ToTable();
}
您可以使用LINQ-to-DataSet
:
dt = dt.AsEnumerable()
.OrderBy(r => r.Field<string>(0) == "Special Value")
.ThenBy(r => r.Field<string>(0))
.CopyToDataTable();
之所以可行,是因为比较结果返回true
或false
并且false
为“ lower”。
如果有人只是想知道如何将表中的行移动到另一个索引,如标题所示: 将数据行添加到预定义索引的数据表中
我将通过单列中的字符串值来标识此行。
关于什么:
dv.Sort = "ParticularColumn ASC, " + dt.Columns[0].ColumnName + " ASC">
其中“ ParticularColumn”是一列,除了您要排在最后的那一行外,所有行的值都相同,且低-该行的值较高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.