Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

DataGridView column footer c#.net winforms

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 label to display the total, but I want to change it to footer if possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ran into the same problem previously and after a long search I realised;

  1. Winform Datagridview do not support adding footer to it.

  2. I tried adding an extra row that could hold the summary but still did not work out fine.

  3. You can create a user control that has two grids and with the lower grid holding the summary.

Solution-- enter image description here

  1. My solution that used data binding.(1)-I Create an abstract object Item with (Name, Cost) properties.(2)-I Create a Concrete item i.e ConcItem that inherit Item(3)-I create a footer item i.e FooterItem that also inherits Item(4)-A collection of Items i.e ItemList where you instantiate the footer item.(5) Finally, just before you do data binding call the method that adds the footer item.

    public abstract class Item
    {
      public virtual string Name { get; set; }
      public virtual int Cost { get; set; }
    }
    public  class ConcItem:Item
    {
      public override string Name { get; set; }
      public override int Cost { get; set; }        
    }
    public  class FooterItem:Item 
    {
      public override string Name { get { return "Total"; } }
      public override int Cost { get; set; }
    }
    public class ItemList : List<Item>
    {
      private Item _footer;
    
      public void SetFooter()
      {
        _footer = new FooterItem();            
        foreach (var item in this)
        {
          _footer.Cost += item.Cost;              
        }
        this.Add(_footer);
      }
    }
    
    
    public partial class Form1 : Form
    {
      Item _item;
      ItemList _itemList;
      public Form1()
      {
        InitializeComponent();
        dgv.DataBindingComplete += dgv_DataBindingComplete;
        _itemList = new ItemList();
    
        SetSampleData();
      }
      private void SetSampleData()
      {
        _item = new ConcItem();
        _item.Name = "Book";
        _item.Cost = 250;
        _itemList.Add(_item);
    
        _item = new ConcItem();
        _item.Name = "Table";
        _item.Cost = 500;
        _itemList.Add(_item);
    
        _item = new ConcItem();
        _item.Name = "PC";
        _item.Cost = 700;
        _itemList.Add(_item);
    
        dgv.DataSource = null;
        _itemList.SetFooter();  //Add the footer item b4  data binding
        dgv.DataSource = _itemList;
      }
      void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
      {
        //If you want to do some formating on the footer row
        int rowIndex = dgv.Rows.GetLastRow(DataGridViewElementStates.Visible);
        if (rowIndex <= 0)
        {
          return;
        }
        dgv.Rows[rowIndex].DefaultCellStyle.BackColor = Color.Red;
        dgv.Rows[rowIndex].DefaultCellStyle.SelectionBackColor = Color.Red;        
        dgv.Rows[rowIndex].DefaultCellStyle.Font = new Font("Microsoft Sans Serif", 12f,    FontStyle.Bold);
      }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...