Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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将行添加到foreach循环内的DataTable中_C#_Csv_Datatable_Dataset_Datarow - Fatal编程技术网

C# C将行添加到foreach循环内的DataTable中

C# C将行添加到foreach循环内的DataTable中,c#,csv,datatable,dataset,datarow,C#,Csv,Datatable,Dataset,Datarow,我正在尝试将文本文件中的变量添加到数据表中,以转换为CSV文件,但我不断收到此错误:名为“机器编号”的列已属于此数据表。我不确定要做什么,请提供任何帮助,谢谢: 代码: }您只需在foreach循环外部声明datatable并添加数据列一次 //Prepare Datatable and Add All Columns Here dataTable = new DataTable(); column = new DataColumn();

我正在尝试将文本文件中的变量添加到数据表中,以转换为CSV文件,但我不断收到此错误:名为“机器编号”的列已属于此数据表。我不确定要做什么,请提供任何帮助,谢谢:

代码:


}

您只需在foreach循环外部声明datatable并添加数据列一次

       //Prepare Datatable and Add All Columns Here
        dataTable = new DataTable();
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "Machine Number";
        column.ReadOnly = false;
        column.Unique = true;
        column.AutoIncrement = false;

   //Excel Input and Dex File Data Marriage
    foreach (string Line in ExcelLines)
    {
     //Add new row and assign values to columns, no need to add columns again and again in loop which will throw exception
     row = dataTable.NewRow();

     //Map all the values in the columns
     row["ColumnName"]= value;

     //At the end just add that row in datatable
     dataTable.Rows.Add(row );

      }

我看到您每次在foreach循环中都向DataTable数据添加列
因此,尝试将这些列添加到循环之外

您只需添加一次列,然后添加它下面的所有行。您正在为每一行添加列。这样做会给我一个csv文件,其中只有标题所有标记为机器类型谢谢,是Data.ImportRow使用Data.rows.Add处理了行
    private void sBAdd_Click(object sender, EventArgs e)
    {

            DataTable dt = new DataTable();
            dt.Columns.Add("MonthlyActualPeriod1", typeof(System.Int32));
            dt.Columns.Add("MonthlyActualPeriod2", typeof(System.Int32));
            dt.Columns.Add("YearlyActualYear1", typeof(System.Int32));
            dt.Columns.Add("YearlyActualYear2", typeof(System.Int32));
            dt.Columns.Add("MonthlyBudgetPeriod1", typeof(System.Int32));
            dt.Columns.Add("MonthlyBudgetPeriod2", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetYear1", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetYear2", typeof(System.Int32));
            dt.Columns.Add("MonthlyActualCurrentPeriod", typeof(System.Int32));
            dt.Columns.Add("YearlyActualCurrentyear", typeof(System.Int32));
            dt.Columns.Add("YearlyActualPrioryear", typeof(System.Int32));
            dt.Columns.Add("MonthlyBudgetCurrentPeriod", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetCurrentyear", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetPrioryear", typeof(System.Int32));                
            for (int i = 1; i < 61; i++)
            {
                dt.Columns.Add("MonthlyActualCurrentPeriod-" + i, typeof(System.Int32));                
                dt.Columns.Add("MonthlyBudgetCurrentPeriod-" + i, typeof(System.Int32));
            }

            int j = dt.Columns.Count;
            DataRow row;
            foreach (DataColumn cl in dt.Columns)
            {
                row = dt.NewRow();

               for (int i = 0; i < j; i++)
                {
                    row[i] = 1;
                }
                dt.Rows.Add(row);
            }             

            this.gcCalcFields.DataSource = dt;

        // Create an unbound column.
        GridColumn unbColumn = gridView1.Columns.AddField("CalcFields");
        unbColumn.VisibleIndex = gridView1.Columns.Count;
        unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Object;

        ColumnFilterMode prev = unbColumn.FilterMode;
        unbColumn.FilterMode = ColumnFilterMode.Value;
        gridView1.ShowUnboundExpressionEditor(unbColumn);
        unbColumn.FilterMode = prev;

        string Calculation = "";

        Calculation = unbColumn.UnboundExpression;
        LBCCalcFieldsActual.Items.Add(Calculation);

        gridView1.Columns.Remove(gridView1.Columns["CalcFields"]);
    }
    private void sBAdd_Click(object sender, EventArgs e)
    {

            DataTable dt = new DataTable();
            dt.Columns.Add("MonthlyActualPeriod1", typeof(System.Int32));
            dt.Columns.Add("MonthlyActualPeriod2", typeof(System.Int32));
            dt.Columns.Add("YearlyActualYear1", typeof(System.Int32));
            dt.Columns.Add("YearlyActualYear2", typeof(System.Int32));
            dt.Columns.Add("MonthlyBudgetPeriod1", typeof(System.Int32));
            dt.Columns.Add("MonthlyBudgetPeriod2", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetYear1", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetYear2", typeof(System.Int32));
            dt.Columns.Add("MonthlyActualCurrentPeriod", typeof(System.Int32));
            dt.Columns.Add("YearlyActualCurrentyear", typeof(System.Int32));
            dt.Columns.Add("YearlyActualPrioryear", typeof(System.Int32));
            dt.Columns.Add("MonthlyBudgetCurrentPeriod", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetCurrentyear", typeof(System.Int32));
            dt.Columns.Add("YearlyBudgetPrioryear", typeof(System.Int32));                
            for (int i = 1; i < 61; i++)
            {
                dt.Columns.Add("MonthlyActualCurrentPeriod-" + i, typeof(System.Int32));                
                dt.Columns.Add("MonthlyBudgetCurrentPeriod-" + i, typeof(System.Int32));
            }

            int j = dt.Columns.Count;
            DataRow row;
            foreach (DataColumn cl in dt.Columns)
            {
                row = dt.NewRow();

               for (int i = 0; i < j; i++)
                {
                    row[i] = 1;
                }
                dt.Rows.Add(row);
            }             

            this.gcCalcFields.DataSource = dt;

        // Create an unbound column.
        GridColumn unbColumn = gridView1.Columns.AddField("CalcFields");
        unbColumn.VisibleIndex = gridView1.Columns.Count;
        unbColumn.UnboundType = DevExpress.Data.UnboundColumnType.Object;

        ColumnFilterMode prev = unbColumn.FilterMode;
        unbColumn.FilterMode = ColumnFilterMode.Value;
        gridView1.ShowUnboundExpressionEditor(unbColumn);
        unbColumn.FilterMode = prev;

        string Calculation = "";

        Calculation = unbColumn.UnboundExpression;
        LBCCalcFieldsActual.Items.Add(Calculation);

        gridView1.Columns.Remove(gridView1.Columns["CalcFields"]);
    }