DataGridView column footer c#.net winforms

前端 未结 3 973
走了就别回头了
走了就别回头了 2020-12-06 03:16

Is there a way to add a column footer in a datagridview which is not databound? I am using it to take user input for adding inventory. Currently I am using a la

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 04:00

    In one of my applications I solved it by taking advantage of NewRow of DataGridView like this.

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                this.dataGridView1.CellFormatting += dataGridView1_CellFormatting;
                this.dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
            }
    
            void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex != this.dataGridView1.NewRowIndex && e.ColumnIndex == 2)
                {
                    this.dataGridView1.InvalidateRow(this.dataGridView1.NewRowIndex);
                }
            }
    
            void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                if (e.RowIndex == this.dataGridView1.NewRowIndex)
                {
                    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
                    e.CellStyle.ForeColor = Color.Red;
                    switch (e.ColumnIndex)
                    {
                        case 0:
                            e.Value = "Total";
                            break;
    
                        case 2:
                            var sum = 0.0d;
                            for (int i = 0; i < this.dataGridView1.NewRowIndex; i++)
                            {
                                var value = this.dataGridView1[2, i].Value;
                                if (value is double)
                                {
                                    sum += ((double)value);
                                }
                            }
                            e.Value = Math.Round(sum, 2);
                            break;
                        // Single line version of case 2 would be
                        // e.Value = this.dataGridView1.Rows.Cast().Where(a => a.Index != a.DataGridView.NewRowIndex).Select(a => (double)a.Cells[2].Value).Sum().ToString("N2");
                    }
                }
            }
    
        }
    }
    

    Here is live screenshot of how it works.

提交回复
热议问题