DataGridView 列页脚 c#.net winforms [英] DataGridView column footer c#.net winforms

查看:22
本文介绍了DataGridView 列页脚 c#.net winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在非数据绑定的 datagridview 中添加列页脚?我正在使用它来获取用户输入以添加库存.目前我正在使用标签来显示总数,但如果可能,我想将其更改为页脚.

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.

推荐答案

我之前遇到过同样的问题,经过长时间的搜索我意识到;

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

  1. Winform Datagridview 不支持向其添加页脚.

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

我尝试添加一个可以保存摘要的额外行,但仍然无法正常工作.

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

您可以创建一个用户控件,它有两个网格,下面的网格包含摘要.

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

解决方案--

  1. 我使用数据绑定的解决方案.(1)-我创建了一个抽象对象 Item 具有(名称、成本)属性.(2)-我创建了一个具体的项目,即 ConcItem 继承了 Item(3)-我创建了一个页脚项,即 FooterItem 也继承了 Item(4)-AItems 集合,即 ItemList,在其中实例化页脚项.(5) 最后,在进行数据绑定之前,调用添加页脚项的方法.

  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);
  }
}

这篇关于DataGridView 列页脚 c#.net winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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