Datatable 增加一列并为该列赋上相应的值

时间:2022-06-17 09:50:28

 自已在开发过程中遇到的问题,纪录下来,以备用

            DataTable table = SMRSCls.ReportCenter.ListCountSum(Keyword);
            table.Columns.Add("PriceSum", Type.GetType("System.Single"));//向table里增加多一列
              int RowsCount = table.Rows.Count;
            for (int j = 0; j < RowsCount; j++)//为该列增加相应的数值
            {
                int CustomerID = Convert.ToInt32(table.Rows[j]["CustomerID"].ToString());
                int ProjectID = Convert.ToInt32(table.Rows[j]["ProjectID"].ToString());
                int Account = SMRSCls.ReportCenter.AccountPrice(CustomerID,ProjectID);
                table.Rows[j]["PriceSum"] = Account;
            }

或者:

            DataTable dt = new DataTable();
            dt.Columns.Add("Name1", typeof(int));
            dt.Columns.Add("Name2", typeof(object));
            dt.Columns.Add("Name3", typeof(object));
            for (int i = 0; i < 200; i++)
            {
                dt.Rows.Add( i, "款号" + Convert.ToString(i), "色号" + Convert.ToString(i));

            }
            gridControl1.DataSource = dt;