简体   繁体   English

以编程方式将新列添加到DataGridView

[英]Programmatically add new column to DataGridView

I have a DataGridView bound to a DataTable. 我有一个绑定到DataTable的DataGridView。 The DataTable is populated from a database query. DataTable是从数据库查询填充的。 The table contains a column named BestBefore. 该表包含一个名为BestBefore的列。 BestBefore is a date formatted as a string (SQLite doesn't have date types). BestBefore是格式化为字符串的日期(SQLite没有日期类型)。

I would like to programmatically add a new column to the DataGridView called Status. 我想以编程方式向DataGridView添加一个名为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. 如果BestBefore小于当前日期,则Status值应设置为OK,否则Status值应设置为NOT OK。

I'm very new to Winforms, so some example code would be greatly appreciated. 我是Winforms的新手,所以我们非常感谢一些示例代码。

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? 我认为DataColumn.Expression可以进行简单的计算,例如将列的整数值乘以另一个值,但是如何做我需要做的事情呢? 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. 也就是说,计算nowBefore列中now和date(字符串格式化)之间的差异,以确定为新状态列提供的值。 Example code would be appreciated. 示例代码将不胜感激。

Keep it simple 把事情简单化

dataGridView1.Columns.Add("newColumnName", "Column Name in Text");

To add rows 添加行

dataGridView1.Rows.Add("Value for column#1"); // [,"column 2",...]

Add new column to DataTable and use column Expression property to set your Status expression. 将新列添加到DataTable并使用列Expression属性来设置Status表达式。

Here you can find good example: DataColumn.Expression Property 在这里你可以找到很好的例子: DataColumn.Expression属性

DataTable and DataColumn Expressions in ADO.NET - Calculated Columns ADO.NET中的DataTable和DataColumn表达式 - 计算列

UPDATE 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 更新#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"));

Here's a sample method that adds two extra columns programmatically to the grid view: 这是一个示例方法,它以编程方式向网格视图添加两个额外的列:

    private void AddColumnsProgrammatically()
    {
        // I created these columns at function scope but if you want to access 
        // easily from other parts of your class, just move them to class scope.
        // E.g. Declare them outside of the function...
        var col3 = new DataGridViewTextBoxColumn();
        var col4 = new DataGridViewCheckBoxColumn();

        col3.HeaderText = "Column3";
        col3.Name = "Column3";

        col4.HeaderText = "Column4";
        col4.Name = "Column4";

        dataGridView1.Columns.AddRange(new DataGridViewColumn[] {col3,col4});
    }

A great way to figure out how to do this kind of process is to create a form, add a grid view control and add some columns. 了解如何执行此类过程的一种好方法是创建表单,添加网格视图控件并添加一些列。 (This process will actually work for ANY kind of form control. All instantiation and initialization happens in the Designer.) Then examine the form's Designer.cs file to see how the construction takes place. (此过程实际上适用于任何类型的表单控件。所有实例化和初始化都在Designer中进行。)然后检查表单的Designer.cs文件以查看构造是如何进行的。 (Visual Studio does everything programmatically but hides it in the Form Designer.) (Visual Studio以编程方式执行所有操作,但将其隐藏在“表单设计器”中。)

For this example I created two columns for the view named Column1 and Column2 and then searched Form1.Designer.cs for Column1 to see everywhere it was referenced. 对于此示例,我为名为Column1和Column2的视图创建了两列,然后在Form1.Designer.cs中搜索Column1以查看它被引用的所有位置。 The following information is what I gleaned and, copied and modified to create two more columns dynamically: 以下信息是我收集,复制和修改以动态创建两列的信息:

// Note that this info scattered throughout the designer but can easily collected.

        System.Windows.Forms.DataGridViewTextBoxColumn Column1;
        System.Windows.Forms.DataGridViewCheckBoxColumn Column2;

        this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.Column2 = new System.Windows.Forms.DataGridViewCheckBoxColumn();

        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column1,
        this.Column2});

        this.Column1.HeaderText = "Column1";
        this.Column1.Name = "Column1";

        this.Column2.HeaderText = "Column2";
        this.Column2.Name = "Column2";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM