11

好的,我现在假设很简单,你有一个多维数组,我尝试使用以下代码填充我的数据表:

System.Data.DataTable _myDataTable =new System.Data.DataTable();    
for (int j=0; j < ele; j++)
{       
        _myDataTable.Columns.Add();   

        for (int i = 0; i < caract+1; i++)
        {
            row[i]=(datar[j,i].ToString());

        }
        _myDataTable.Rows.Add(row);

}

我的数组名称是datar但我收到的错误:

 System.IndexOutOfRangeException: cant find column 1.

我究竟做错了什么?顺便说一句:我使用的是 C#、asp.net,而不是 Visual Studio。

4

6 回答 6

22

正如chiffre所指出的,您实际上有 3 个问题:您必须先添加所有列,然后才能开始添加行,并且您必须先创建一个DataRow,然后才能将其添加到您的DataTable. 您的第三个问题是您的行维计数器caract+1,它将产生一个 IndexOutOfRange 异常。

DataTable _myDataTable = new DataTable();

// create columns
for (int i = 0; i < ele; i++)
{
    _myDataTable.Columns.Add();
}

for (int j = 0; j < caract; j++)
{
    // create a DataRow using .NewRow()
    DataRow row = _myDataTable.NewRow();

    // iterate over all columns to fill the row
    for (int i = 0; i < ele; i++)
    {
        row[i] = datar[i, j];
    }

    // add the current row to the DataTable
    _myDataTable.Rows.Add(row);
}
于 2012-07-04T15:39:03.723 回答
5

扩展方法怎么样

static class HappyExtEnding
{
    public static DataTable ToDataTable<T>(this T [] students)
    {
        if (students == null || students.Length == 0) return null;

        DataTable table = new DataTable();
        var student_tmp = students[0];
        table.Columns.AddRange(student_tmp.GetType().GetFields().Select(field => new DataColumn(field.Name, field.FieldType)).ToArray());
        int fieldCount = student_tmp.GetType().GetFields().Count();

        students.All(student =>
        {
            table.Rows.Add(Enumerable.Range(0, fieldCount).Select(index => student.GetType().GetFields()[index].GetValue(student)).ToArray());
            return true;
        });

        return table;
    }
}

用法

Student[] students = {
     new Student { Id = 1, Name = "Joe Rattz", Address = "Sriram Apartments" },
     new Student { Id = 6, Name = "Ulyses Hutchens", Address = "Sriram Apartments" },
     new Student { Id = 19, Name = "Bob Tanko", Address = "Sriram Apartments" },
     new Student { Id = 45, Name = "Erin Doutensal", Address = "Sriram Apartments" },
     new Student { Id = 1, Name = "Joe Rattz", Address = "Sriram Apartments" },
     new Student { Id = 12, Name = "Bob Mapplethorpe", Address = "Sriram Apartments" },
     new Student { Id = 17, Name = "Anthony Adams", Address = "Sriram Apartments" },
     new Student { Id = 32, Name = "Dignan Stephens Mark", Address = "Sriram Apartments" },
     new Student { Id = 1232, Name = "Dignan Stephens", Address = "Sriram Apartments Henry Labamba Beligi" },
     new Student { Id = 132, Name = "Neha Dhupia", Address = "Sriram Apartments 123456" },
     new Student { Id = 132, Name = "", Address = "Sriram Apartments 123456" },
     new Student { Id = 133, Name = "", Address = "Sriram Apartments 123456" },
     new Student { Id = 134, Name = "Neha Dhupia", Address = "" },
     new Student { Id = 134, Name = "Shradha Kapoor", Address = "Mumbai" }
 };

 //ParallelQuery<int>

 DataTable dtTmp = students.ToDataTable() ;
于 2015-02-19T07:56:38.717 回答
2

一些问题:

  1. 您的代码不添加任何列
  2. 您的代码不添加任何行

一步一步进行。

  1. 添加列。

    //using System.Data
    DataTable _myDataTable = new DataTable();
    _myDataTable.Columns.Add(new DataColumn("Field_1"));
    _myDataTable.Columns.Add(new DataColumn("Field_2"));
    //...
    _myDataTable.Columns.Add(new DataColumn("Field_Ele"));
    
  2. 添加行。您需要从 _myDataTable 创建一个新的 Datarow,用数据填充它,将其添加到表中:

    //assuming your data are stored in datar[,]
    String[,] datar = new String[max_i, max_j];
    for (int i = 0; i < ele; i++)
    {
        DataRow r = _myDataTable.NewRow();
        for (int j = 0; j < caract; j++)
        {
            //mind casting issues
            r[j] = datar[i, j];
        }
        _myDataTable.rows.add(r);
    }
    

有关详细信息,请参见此处

于 2012-07-04T15:45:28.613 回答
1
public static DataSet MultidimensionalArrayToDataSet(string[,] input)
{
    var dataSet = new DataSet();
    var dataTable = dataSet.Tables.Add();
    var iFila = input.GetLongLength(0);
    var iCol = input.GetLongLength(1);

    //Fila
    for (var f = 1; f < iFila; f++)
    {
        var row = dataTable.Rows.Add();
        //Columna
        for (var c = 0; c < iCol; c++)
        {
            if (f == 1) dataTable.Columns.Add(input[0, c]);
            row[c] = input[f, c];
        }
    }
    return dataSet;
}
于 2013-08-15T18:03:32.413 回答
1

如果您有一个数组,其中第一行具有 DataTable 列名,则可以使用。

    public static System.Data.DataTable ArrayToDataTable(Array array, bool headerQ=true)
    {
        if (array == null || array.GetLength(1) == 0 || array.GetLength(0) == 0) return null;
        System.Data.DataTable dt = new System.Data.DataTable();
        int dataRowStart = headerQ ? 1 : 0;

        // create columns
        for (int i = 1; i <= array.GetLength(1); i++)
        {
            var column = new DataColumn();
            string value = array.GetValue(1, i) is System.String
                ? array.GetValue(1, i).ToString() : "Column" + i.ToString();

            column.ColumnName = value;
            dt.Columns.Add(column);
        }
        if (array.GetLength(0) == dataRowStart) return dt;  //array has no data

        //Note:  the array is 1-indexed (not 0-indexed)
        for (int i = dataRowStart + 1; i <= array.GetLength(0); i++)
        {
            // create a DataRow using .NewRow()
            DataRow row = dt.NewRow();

            // iterate over all columns to fill the row
            for (int j = 1; j <= array.GetLength(1); j++)
            {
                row[j-1] = array.GetValue(i,j);
            }

            // add the current row to the DataTable
            dt.Rows.Add(row);
        }
        return dt;
    }
于 2014-08-22T23:40:08.227 回答
-1
        var dt = new DataTable();
        var iFila = vals.GetLongLength(0);
        var iCol = vals.GetLongLength(1);

        for (var f = 1; f < iFila; f++)
        {
            var row = dt.Rows.Add();
            for (var c = 1; c <= iCol; c++)
            {
                if (f == 1) 
                    dt.Columns.Add(vals[1, c] != null 
                        ? vals[1, c].ToString() 
                        : "");

                row[vals[1, c].ToString()] = vals[f, c];
            }
        }
于 2015-02-11T08:51:31.300 回答