Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以使用c#在数据集的中间添加一行吗?_C#_Dataset - Fatal编程技术网

我可以使用c#在数据集的中间添加一行吗?

我可以使用c#在数据集的中间添加一行吗?,c#,dataset,C#,Dataset,是否可以使用c#在现有数据集的中间添加一行?我已经做了很多搜索,但没有找到任何关于如何做到这一点的信息。我试过什么?我尝试了很多次搜索,但没有找到类似于数据集的“insertAt”方法。 谢谢 Mike在我看来(虽然这可能需要很多时间),您可以创建一个数组或列表数组,然后通过for循环或任意循环传输数据集中的所有数据……然后在其中放入if语句,检查您希望将额外数据放在何处,如下所示: List<string> arrayList = dataset;// i know this is

是否可以使用c#在现有数据集的中间添加一行?我已经做了很多搜索,但没有找到任何关于如何做到这一点的信息。我试过什么?我尝试了很多次搜索,但没有找到类似于数据集的“insertAt”方法。 谢谢 Mike

在我看来(虽然这可能需要很多时间),您可以创建一个数组或列表数组,然后通过for循环或任意循环传输数据集中的所有数据……然后在其中放入if语句,检查您希望将额外数据放在何处,如下所示:

List<string> arrayList = dataset;// i know this is not possible just showing you that you have to put all your data from dataset to array:)
List <string> newList = new List<string>();//its up to you if you want to put another temporary array or you could simply output your data from the loop.
//THE LOOP
for(int i = 0; i<=arrayList.Count(); i++){
    if(i == x)//x is the index or you may change this statement its up to you
  {
    //do the print or pass the data to newList
      newList.add(arraList[i]);//not sure about this. its been a while since the last time i use this array list..
  }
}
List arrayList=dataset;//我知道这是不可能的,只是告诉您必须将所有数据从数据集放到数组:)
List newList=新列表()//如果您想放置另一个临时数组,或者您可以简单地从循环中输出数据,这取决于您。
//环路
对于(int i=0;i,在我看来(虽然这可能需要很多时间),您可以创建一个数组或列表数组,然后通过for循环或任意循环传输数据集中的所有数据……然后在其中放入一个if语句,检查您希望将额外数据放在何处,如下所示:

List<string> arrayList = dataset;// i know this is not possible just showing you that you have to put all your data from dataset to array:)
List <string> newList = new List<string>();//its up to you if you want to put another temporary array or you could simply output your data from the loop.
//THE LOOP
for(int i = 0; i<=arrayList.Count(); i++){
    if(i == x)//x is the index or you may change this statement its up to you
  {
    //do the print or pass the data to newList
      newList.add(arraList[i]);//not sure about this. its been a while since the last time i use this array list..
  }
}
List arrayList=dataset;//我知道这不可能只是告诉您必须将所有数据从数据集放到数组:)
List newList=新列表()//如果您想放置另一个临时数组,或者您可以简单地从循环中输出数据,这取决于您。
//环路

对于(int i=0;i数据集由一组数据表对象组成,因此我假设您正在谈论一个数据表,对吗?如果是这样,它有一个
InsertAt
方法:

DataTable dt = dataset.Tables[0]; //Get first datatable from dataset
DataRow row = dt.NewRow();
//fill row
dt.Rows.InsertAt(row,3); //Insert at index 3

数据集由一组DataTable对象组成,因此我假设您谈论的是一个DataTable,对吗?如果是,它有一个
InsertAt
方法:

DataTable dt = dataset.Tables[0]; //Get first datatable from dataset
DataRow row = dt.NewRow();
//fill row
dt.Rows.InsertAt(row,3); //Insert at index 3

数据集没有行集合,因此您根本无法向其中添加行


您可以使用
DataTable.Rows.InsertAt(row,i)
将一行按索引插入到DataTable对象中。如果表位于数据集中,您的语法将是
DataSet.Tables[i].Rows.InsertAt(row,0)
数据集没有行集合,因此您根本无法向其中添加行


您可以使用
DataTable.Rows.InsertAt(row,i)
将一行按索引插入到DataTable对象中。如果该表位于数据集中,您的语法将是
DataSet.Tables[i].Rows.InsertAt(row,0)
,下面是一个简短的示例:

class Program
{
    static void Main(string[] args)
    {
        DataSet ds = new DataSet();

        DataTable dt = ds.Tables.Add("Table");
        dt.Columns.Add("Id");
        for (int i = 0; i < 10; i++)
        {
            dt.Rows.Add(new object[]{i});
        }

        var newRow=dt.NewRow();
        newRow.ItemArray=new string[]{(dt.Rows.Count/2).ToString()+".middle"};
        dt.Rows.InsertAt(newRow, dt.Rows.Count / 2);
    }
}
类程序
{
静态void Main(字符串[]参数)
{
数据集ds=新数据集();
DataTable dt=ds.Tables.Add(“表”);
dt.列。添加(“Id”);
对于(int i=0;i<10;i++)
{
Add(新对象[]{i});
}
var newRow=dt.newRow();
newRow.ItemArray=新字符串[]{(dt.Rows.Count/2).ToString()+“.middle”};
dt.Rows.InsertAt(newRow,dt.Rows.Count/2);
}
}

以下是一个简短的示例:

class Program
{
    static void Main(string[] args)
    {
        DataSet ds = new DataSet();

        DataTable dt = ds.Tables.Add("Table");
        dt.Columns.Add("Id");
        for (int i = 0; i < 10; i++)
        {
            dt.Rows.Add(new object[]{i});
        }

        var newRow=dt.NewRow();
        newRow.ItemArray=new string[]{(dt.Rows.Count/2).ToString()+".middle"};
        dt.Rows.InsertAt(newRow, dt.Rows.Count / 2);
    }
}
类程序
{
静态void Main(字符串[]参数)
{
数据集ds=新数据集();
DataTable dt=ds.Tables.Add(“表”);
dt.列。添加(“Id”);
对于(int i=0;i<10;i++)
{
Add(新对象[]{i});
}
var newRow=dt.newRow();
newRow.ItemArray=新字符串[]{(dt.Rows.Count/2).ToString()+“.middle”};
dt.Rows.InsertAt(newRow,dt.Rows.Count/2);
}
}

定义中间。可能您指的是
数据集中的
DataTable
。DataSet.Tables[0]是DataTable吗?我可能会感到困惑。即使如此,还是定义“中间”对于
数据表
。不是最后一条或第一条记录的任何位置。我将在数据表的其他行之间添加几行。定义中间。可能您指的是
数据集
中的
数据表
。数据集是吗?表[0]是数据表吗?我可能会感到困惑。即使如此,定义“中间”对于
数据表
。不是最后一条或第一条记录的任何位置。我将在数据表的其他行之间添加几行。这也是正确的,我只是将mallan的标记为答案,因为他回答了first@dmikester1他没有,但没关系;)这也是正确的,我只是把mallan的作为答案因为他回答了first@dmikester1他没有,但没关系;)