编程新列添加到DataGridView的 [英] Programatically add new column to DataGridView
问题描述
我绑定到一个DataTable一个DataGridView。该数据表从数据库查询填充。该表包含一个名为BestBefore列。 BestBefore被格式化为一个字符串的日期(SQLite没有日期类型)。
I have a DataGridView bound to a DataTable. The DataTable is populated from a database query. The table contains a column named BestBefore. BestBefore is a date formatted as a string (SQLite doesn't have date types).
我想一个新列编程方式添加到一个名为状态DataGridView的。如果BestBefore小于当前日期,状态值应设置为OK,否则状态值应设置为不正常。
I would like to programatically add a new column to the DataGridView called Status. If BestBefore is less than the current date, Status value should be set to OK, otherwise Status value should be set to NOT OK.
我很新的WinForms,所以一些例如code将大大AP preciated。
I'm very new to Winforms, so some example code would be greatly appreciated.
更新:
我觉得DataColumn.Ex pression没事做简单的计算,例如由另一个值乘以列的整数值,而是做我需要做什么?也就是说,计算现在和BestBefore列中的日期(格式字符串)之间的差额,以确定什么样的价值给新的状态栏。例如code将AP preciated。
I think DataColumn.Expression is okay for doing simple calculations such multiplying a column's integer value by another value, but what about doing what I need to do? That is, calculate the difference between now and the date (string formatted) in the BestBefore column to determine what value to give the new status column. Example code would be appreciated.
感谢。
推荐答案
添加新列数据表
和使用列防爆pression
属性来设置你的状态的前pression。
Add new column to DataTable
and use column Expression
property to set your Status expression.
在这里,你可以找到很好的例子:<一href=\"http://msdn.microsoft.com/en-us/library/system.data.datacolumn.ex$p$pssion.aspx\"><$c$c>DataColumn.Ex$p$pssion$c$c>物业
Here you can find good example: DataColumn.Expression
Property
在ADO.NET 的DataTable和DataColumn的前pressions - 计算列
更新
code样品:</ P>
Code sample:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("colBestBefore", typeof(DateTime)));
dt.Columns.Add(new DataColumn("colStatus", typeof(string)));
dt.Columns["colStatus"].Expression = String.Format("IIF(colBestBefore < #{0}#, 'Ok','Not ok')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
dt.Rows.Add(DateTime.Now.AddDays(-1));
dt.Rows.Add(DateTime.Now.AddDays(1));
dt.Rows.Add(DateTime.Now.AddDays(2));
dt.Rows.Add(DateTime.Now.AddDays(-2));
demoGridView.DataSource = dt;
更新#2
dt.Columns["colStatus"].Expression = String.Format("IIF(CONVERT(colBestBefore, 'System.DateTime') < #{0}#, 'Ok','Not ok')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
这篇关于编程新列添加到DataGridView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!