简述:DataGrid控件显示List集合数据信息,将开始、结束时间、其他查询字段通过POST接口获取接口返回的List集合数据;代码如下:
DataGrid列表页:
private void BindData()
{
Dictionary<string, string> addParames = new Dictionary<string, string>();
addParames.Add("beginDate", begin_date.SelectedDate.Value.ToString("yyyy-MM-dd"));
addParames.Add("endDate", end_date.SelectedDate.Value.AddDays(1).ToString("yyyy-MM-dd"));
addParames.Add("ChePai", chepai.Text.Trim());
addParames.Add("Lydh", lydh.Text.Trim());
try
{
string PostUrl = "http://192.168.1.150:8089/api/RiZhi/GetRiZhiInfoByModel";
string sendData = JsonConvert.SerializeObject(addParames);//发送集合
RecordLog(sendData);
string resultData = Post(sendData, PostUrl);
List<Sys_RiZhi> riZhiList = JsonConvert.DeserializeObject<List<Sys_RiZhi>>(resultData);//josn集合转成list集合
dataGrid1.ItemsSource = riZhiList;
//RecordLog(resultData);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("局域网链接失败:{0}", ex.Message));
}
}
接口:
public List<RiZhi> GetRiZhiInfoByModel(dynamic myRequest)
{
DateTime begin_date = DateTime.Now;
DateTime endDate_date = DateTime.Now;
if (!string.IsNullOrEmpty(myRequest.beginDate.ToString())) { begin_date = Convert.ToDateTime(myRequest.beginDate.ToString()); }
if (!string.IsNullOrEmpty(myRequest.endDate.ToString())) { endDate_date = Convert.ToDateTime(myRequest.endDate.ToString()); }
string sqlStr = "select * from Sys_RiZhi where CreateTime>='" + begin_date.ToString("yyyy-MM-dd") + "' and CreateTime<='" + endDate_date.AddDays(1).ToString("yyyy-MM-dd") + "'";
if (!string.IsNullOrEmpty(myRequest.ChePai.ToString()))
{
sqlStr += " and ChePai like '%" + myRequest.ChePai + "%'";
}
if (!string.IsNullOrEmpty(myRequest.Lydh.ToString()))
{
sqlStr += " and Lydh like '%" + myRequest.Lydh + "%'";
}
DataTable dt = SQLiteHelper.ExecuteDataTable(sqlStr, null);
List<RiZhi> riZhiList = SQLiteHelper.ToList<RiZhi>(dt);
return riZhiList;
}