foreach (System.Reflection.PropertyInfo p in InvoiceList[i].GetType().GetProperties())
{
object value = p.GetValue(InvoiceList[i], null);
string ColumnsValueString="";
if (value!=null)
{
赋值:ColumnsValueString = value.ToString();
}else{
//赋值
}
详例说明:
#region for (var m = 0; m < modelList.Count; m++)//循环同一个单号下的所有明细 { string posLineNo = string.Empty; //单号行号信息 DataRow drNewRow = dtNew.NewRow();//创建新行 unit = modelList[m].规格; //循环读取类属性名,并将属性值赋值给dataTable中与其对应的字段名 foreach (System.Reflection.PropertyInfo p in modelList[m].GetType().GetProperties()) { //判断备注和规格型号 string columnName = p.Name; if (columnName == "PO单号") { //备注栏中没有,则从规格型号里获取 if (OperateType == string.Empty) { //拆分规格中的pos单号信息 PosNoList = new List<PosNoInfo>(); SplitToArr(unit, ref PosNoList); if (PosNoList.Count > 0) { drNewRow[columnName] = PosNoList[0].PosNo; posLineNo = PosNoList[0].PosLineNo; PosNoList = new List<PosNoInfo>(); continue; } } //备注中只有一条po单号 else if (OperateType == "AllSameValue") { drNewRow[columnName] = PosNoList[0].PosNo; posLineNo = PosNoList[0].PosLineNo; continue; } else { //1.po单号等于明细数 2.po单号大于1且po单号小于明细数 3.po单号大于明细数 var model = PosNoList.Where(a => a.LineNo == m+1).ToList(); if (model.Count > 0) { drNewRow[columnName] = model.First().PosNo; posLineNo = model.First().PosLineNo; continue; } } } if (columnName == "PO单号行号") { drNewRow[columnName] = posLineNo; continue; } object value = p.GetValue(modelList[m], null); if (value != null) { drNewRow[columnName] = value.ToString(); } else { drNewRow[columnName] = ""; } } dtNew.Rows.Add(drNewRow);//追加行内容 } #endregion