C# 如何将DataTable行移动到其DataTable的第一个位置

C# 如何将DataTable行移动到其DataTable的第一个位置,c#,asp.net,linq,datatable,dataset,C#,Asp.net,Linq,Datatable,Dataset,我想获取asp.net数据表上的一个特定行,并根据column1列的值将其移动到此数据表上的第一行。我的Datatable dt1是通过一个DB查询填充的,要搜索的值是通过另一个DB的另一个查询填充的,因此我不知道在dt1选择时间要搜索的值 所以我需要在我的DataTable中的column1列中搜索一些值。然后将整行移动到第一个位置 谢谢。如果该列对于搜索值只有一条记录,请尝试此操作 DataRow[] dr = dtEmp.Select("column1 ='" + valueToSearc

我想获取asp.net数据表上的一个特定行,并根据column1列的值将其移动到此数据表上的第一行。我的Datatable dt1是通过一个DB查询填充的,要搜索的值是通过另一个DB的另一个查询填充的,因此我不知道在dt1选择时间要搜索的值

所以我需要在我的DataTable中的column1列中搜索一些值。然后将整行移动到第一个位置


谢谢。

如果该列对于搜索值只有一条记录,请尝试此操作

DataRow[] dr = dtEmp.Select("column1 ='" + valueToSearch +"'");
myDataTable.Rows.InsertAt(dr[0], 0);

您必须在此基础上测试性能,但一种方法是在查询本身中进行测试。首先在顶部获取所需的行,并将其与其余行合并

由于我对您的数据库一无所知,以下是一种通用方法:

SELECT Column1, Column2, Column3
FROM MyTable
WHERE Column1 = 'some value'

UNION

SELECT Column1, Column2, Column3
FROM MyTable
WHERE Column1 <> 'some value'

在执行以下操作之前,我们必须克隆行数据:

            DataRow[] dr = dtable.Select("column1 ='" + valueToSearch +"'");
            DataRow newRow = dtable.NewRow();
            // We "clone" the row
            newRow.ItemArray = dr[0].ItemArray;
            // We remove the old and insert the new
            ds.Tables[0].Rows.Remove(dr[0]);
            ds.Tables[0].Rows.InsertAt(newRow, 0);

这个数据表是如何填充的?@WonkotheSane通过数据库查询。图1中的column1带有字符串值。我不能这样做,因为datatable的源和要搜索的值不同,所以我不知道在查询时要搜索的值。在这种情况下,我认为您应该编辑您的问题以提供更多详细信息。datatable是如何填充的,包括绑定、“某些值”是如何作为筛选器输入的,等等。现在,我们都在猜测您的设计。我无法插入它,因为它已经属于此datatable,所以我需要先删除它。请看一下。如果行有效,则无需删除该行并再次插入。
            DataRow[] dr = dtable.Select("column1 ='" + valueToSearch +"'");
            DataRow newRow = dtable.NewRow();
            // We "clone" the row
            newRow.ItemArray = dr[0].ItemArray;
            // We remove the old and insert the new
            ds.Tables[0].Rows.Remove(dr[0]);
            ds.Tables[0].Rows.InsertAt(newRow, 0);