向DataGridView中编程添加新列 [英] Programatically add new column to DataGridView

查看:106
本文介绍了向DataGridView中编程添加新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView绑定到一个DataTable。 DataTable是从数据库查询填充的。该表包含一个名为BestBefore的列。 BestBefore是格式为字符串的日期(SQLite没有日期类型)。



我想以编程方式向DataGridView添加一个名为Status的列。如果BestBefore小于当前日期,Status值应设置为OK,否则状态值应设置为NOT OK。



我对Winforms很新,所以一些示例代码将不胜感激。



更新:



DataColumn.Expression可以进行简单的计算,例如将列的整数值乘以另一个值,但是做什么我需要做的呢?也就是说,计算现在和BestBefore列中的日期(字符串格式化)之间的差异,以确定给出新状态列的值。示例代码将不胜感激



谢谢。

解决方案

将新列添加到 DataTable 并使用列表达式属性来设置状态表达式。



你可以找到很好的例子: DataColumn .Expression 属性



ADO.NET中的DataTable和DataColumn表达式 - 计算列



更新



代码示例:

  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


$ b $表达式= String.Format(IIF(CONVERT(colBestBefore,'System.DateTime'))<#{0}#,'

OK','not ok'),DateTime.Now.ToString(yyyy-MM-dd HH:mm:ss));


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).

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.

I'm very new to Winforms, so some example code would be greatly appreciated.

UPDATE:

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.

Thanks.

解决方案

Add new column to DataTable and use column Expression property to set your Status expression.

Here you can find good example: DataColumn.Expression Property

DataTable and DataColumn Expressions in ADO.NET - Calculated Columns

UPDATE

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;

UPDATE #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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆