List&LinQ
DataTable会将整个数据表接收过来,可真正使用的数据未必是整个数据表所有的数据。
使用List可以接收需要使用的数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Data_Model { public string Shift { get; set; } public string Line { get; set; } public string SO { get; set; } public string RefNo { get; set; } public string PartNo { get; set; } public string SoQty { get; set; } public string chgSoQty { get; set; } public string PlanStartTime { get; set; } public string WaitMin { get; set; } public string Remark { get; set; } public string StartTime{ get; set; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
List<Data_Model> list = new List<Data_Model>(); for (int m = 0; m < dt.Rows.Count; m++) { Data_Model model = new Data_Model(); model.Shift = dt.Rows[m]["Shift"].ToString(); model.Line = dt.Rows[m]["Line"].ToString(); model.SO = dt.Rows[m]["So"].ToString(); model.RefNo = dt.Rows[m]["RefNo"].ToString(); model.SoQty = dt.Rows[m]["SoQty"].ToString(); model.chgSoQty = dt.Rows[m]["chgSoQty"].ToString(); model.StartTime = dt.Rows[m]["StartTime"].ToString(); model.PlanStartTime = dt.Rows[m]["PlanStartTime"].ToString(); model.WaitMin = dt.Rows[m]["WaitMin"].ToString(); model.Remark = dt.Rows[m]["Remark"].ToString(); list.Add(model); }
定义一个类用来作为接收数据的对象,并且类中含有需要接收的属性。将对象添加到List中。GridTable接受List.
通过List可以选择性的接收部分数据,此外List中的数据也会进行筛选,显示部分满足 条件的信息。
筛选List信息
if (cboLine.Text != "ALL" && cboSO.Text != "ALL" && cboStatus.Text == "ALL") { list = list.Where(a => a.Line.Contains(cboLine.Text) && a.SO.Contains(cboSO.Text)).ToList(); }
筛选List使用的是Where方法,根据下拉框中的筛选包含条件,可多个也可单个。
List获取每行的某个值
例如获取每行的状态,将其改变。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int coun = grdData.RowCount; for (int i = 0; i < coun; i++) { grdData.Rows[i].Cells["RowID"].Value = i + 1; } for (int j = 0; j < coun; j++) { if (grdData.RowCount > 0) { if (list[j].Status.ToString().Equals("")) { strRefStatus = "EMPTY"; } else { strRefStatus = list[j].Status.ToString(); } if (strRefStatus != "FULL") { strStatus = ""; if (strRefStatus == "EMPTY") { strStatus = "备料"; } if (strStatus == "PREPARE") { strStatus = "备料中"; } if (strRefStatus == "WAIT") { strStatus = "等待"; } } } grdData.Rows[j].Cells["Status"].Value = strStatus; }
LinQ查询
LINQ查询 从LinQ查询中生成新的DataTable Static void BuildDataTableFromQuery(DataTable data) { var cars= from car in data.AsEnumerable() where car.id>5 select car;. DataTable newTable =cars.CopyToDataTable();//使用该结果集来构建新的DataTable }