C# XPTable 日期字段处理(XPTable控件使用说明十三)
1、SQLite数据库定义为日期类型
2、XPtable中日期字段定义与显示
//显示时间表
columnModel1.Columns.Clear();
columnModel1.Columns.Add(new NumberColumn("id", 30));
NumberColumn numberColumn = new NumberColumn("次数", 50);
numberColumn.Maximum = 10000;
columnModel1.Columns.Add(numberColumn);
NumberColumn numberColumn2 = new NumberColumn("公里", 80);
numberColumn2.Maximum = 9999999;
columnModel1.Columns.Add(numberColumn2);
DateTimeColumn dateTimeColumn = new DateTimeColumn("日期", 120);
dateTimeColumn.CustomDateTimeFormat = "yyyy.MM.dd";
columnModel1.Columns.Add(dateTimeColumn);
tableModel1.Rows.Clear();
foreach (var item in FormHome.db.Times.Where(p => p.carid == one.id).OrderBy(p => p.times))
{
XPTable.Models.Row r = new XPTable.Models.Row();
tableModel1.Rows.Add(r);
r.Cells.Add(new XPTable.Models.Cell(item.id));
r.Cells.Add(new XPTable.Models.Cell(item.times));
r.Cells.Add(new XPTable.Models.Cell(item.km));
r.Cells.Add(new XPTable.Models.Cell(item.zdate.Value));
}
3、保存到数据库中
// 时间编辑停止:id,次数,公里,日期
private void table1_EditingStopped(object sender, XPTable.Events.CellEditEventArgs e)
{
var tmp = FormHome.db.Times.Where(c => c.id == MT.i(tableModel1.Rows[e.Row].Cells[0].Data)).FirstOrDefault();
tmp.carid = one.id;
tmp.times = MT.i(tableModel1.Rows[e.Row].Cells[1].Data);
tmp.km = MT.i(tableModel1.Rows[e.Row].Cells[2].Data);
tmp.zdate = MT.ddd(tableModel1.Rows[e.Row].Cells[3].Data);
FormHome.db.SaveChanges();
FormHome.downtxt.Text = "数据已经保存!";
}
4、自己的函数处理一下转换
public static int i(object x1)
{
return Convert.ToInt32(x1);
}
public static string s(object x1)
{
return Convert.ToString(x1);
}
public static Decimal d(object x1)
{
return Convert.ToDecimal(x1);
}
public static DateTime ddd(object x1)
{
return Convert.ToDateTime(x1);
}