private void SetEstimateViewModels() { EstimateViewModels = new ObservableCollection <ProjectEstimateViewModel>(); string tempName = ""; for (int i = 1; i < dataTable.Rows.Count; i++) { ProjectEstimateViewModel estimateViewModel = new ProjectEstimateViewModel(); PropertyInfo[] propertys = estimateViewModel.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查DataTable是否包含此列 if (dataTable.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) { continue; } object value = dataTable.DefaultView[i][tempName]; if (value != System.DBNull.Value) { pi.SetValue(estimateViewModel, value, null); } } } EstimateViewModels.Add(estimateViewModel); } TotalEstimateViewModel.ProjectName = dataTable.DefaultView[0]["ProjectName"].ToString(); TotalEstimateViewModel.ProjectCode = dataTable.DefaultView[0]["ProjectCode"].ToString(); TotalEstimateViewModel.EstimateNumber = dataTable.DefaultView[0]["EstimateNumber"].ToString(); EstimateViewModels.Insert(0, TotalEstimateViewModel); }
//将obc转换为dt private DataTable TranslateVM2DT() { DataTable dt = new DataTable("Estinates"); ProjectEstimateViewModel temp = new ProjectEstimateViewModel(); PropertyInfo[] propertys = temp.GetType().GetProperties(); ProjectTotalEstimateViewModel temp2 = new ProjectTotalEstimateViewModel(); PropertyInfo[] property2 = temp2.GetType().GetProperties(); dt.Columns.Add("id"); dt.Columns.Add("ID"); dt.Columns.Add("ProjectName"); dt.Columns.Add("ProjectCode"); dt.Columns.Add("IndividualProjectName"); dt.Columns.Add("IndividualProjectCode"); dt.Columns.Add("ExpanseCategory"); dt.Columns.Add("WBSCode"); dt.Columns.Add("EstimateNumber"); dt.Columns.Add("InternalControl"); dt.Columns.Add("DeductibleVATRatio"); dt.Columns.Add("TotalInvestmentWithTax"); dt.Columns.Add("TotalInvestmentWithoutTax"); dt.Columns.Add("MaxInternalControl"); dt.Columns.Add("MaxDeductibleVATRatio"); dt.Columns.Add("MinDeductibleVATRatio"); foreach (ProjectEstimateViewModel t in estimateSetViewModel.EstimateViewModels) { if (t is ProjectTotalEstimateViewModel) { continue; } PropertyInfo[] property = t.GetType().GetProperties(); DataRow dr = dt.NewRow(); foreach (PropertyInfo pi in propertys) { if (!pi.CanRead) { continue; } dr[pi.Name] = pi.GetValue(t, null); } dt.Rows.Add(dr); } DataRow dr2 = dt.NewRow(); foreach (PropertyInfo pi in property2) { if (!pi.CanRead) { continue; } dr2[pi.Name] = pi.GetValue(estimateSetViewModel.TotalEstimateViewModel, null); } dr2["EstimateNumber"] = estimateSetViewModel.TotalEstimateViewModel.EstimateNumber; dr2["TotalInvestmentWithTax"] = estimateSetViewModel.TotalEstimateViewModel.TotalInvestmentWithTax; dr2["TotalInvestmentWithoutTax"] = estimateSetViewModel.TotalEstimateViewModel.TotalInvestmentWithoutTax; dt.Rows.InsertAt(dr2, 0); return(dt); }