一、创建一个空表并加记录
        //PubTruckCustomer打印信息
        public static DataRow GetPubTruckCustomerPrintInfo(string customerNo)
        {
            //定义DataTable结构
            var dataTable = new DataTable();
            dataTable.AddColumn("CustomerNo", "客戶編號", typeof(string));
            dataTable.AddColumn("CustomerName", "客戶名稱", typeof(string));
            dataTable.AddColumn("Tel", "公司電話", typeof(string));
            dataTable.AddColumn("Fax", "公司傳真", typeof(string));

            //加一行记录   
            var dataRow = dataTable.Rows.Add();
            if (!string.IsNullOrEmpty(customerNo))
            {
                StringBuilder cmd = new StringBuilder();
                cmd.AppendLine("select top 1 CustomerNo, CustomerName, Tel, Fax ");
                cmd.AppendLine("from PubTruckCustomer ");
                cmd.AppendLine("where CustomerNo='" + customerNo.ToString() + "'");
                var customer = SmartRemoteAgent.StaticAgent.GetDataRow(cmd.ToString());
                if (customer != null)
                {
                    dataRow["CustomerNo"] = customer.Field<string>("CustomerNo");
                    dataRow["CustomerName"] = customer.Field<string>("CustomerName");
                    dataRow["Tel"] = customer.Field<string>("Tel");
                    dataRow["Fax"] = customer.Field<string>("Fax");
                }
            }
            return dataRow;
        }

二、克隆表结构并加记录
                // 1.取当前数据库中的记录
                var ds = SmartRemoteAgent.StaticAgent.GetDataSet("select * from PubTruckPort where TruckID = '" + this.CurrentItem.Row.Field<Guid>("ID") + "'");
                var dbTable = ds.Tables[0];


                // 2.创建与数据表结构一致的空临时表
                var memoryTable = dbTable.Clone();


                // 3.插入数据到临时表
                foreach (DataRow port in this.Ports.Table.Select("IsSelected = 1"))
                {
                    var newRow = memoryTable.Rows.Add();
                    newRow.SetField("ID", Guid.NewGuid());
                    newRow.SetField("TruckID", this.CurrentItem.Row.Field<Guid>("ID"));
                    newRow.SetField("PortID", port.Field<Guid>("ID"));
                    newRow.SetField("RowStatus", 0);
                }



本文转载:CSDN博客