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
