Programmatically add new column to DataGridView

前端 未结 3 1256
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 09:39

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

相关标签:
3条回答
  • 2020-11-28 10:11

    Keep it simple

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

    To add rows

    dataGridView1.Rows.Add("Value for column#1"); // [,"column 2",...]
    
    0 讨论(0)
  • 2020-11-28 10:17

    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. (Visual Studio does everything programmatically but hides it in the Form Designer.)

    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. 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";
    
    0 讨论(0)
  • 2020-11-28 10:33

    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"));
    
    0 讨论(0)
提交回复
热议问题