有没有办法添加一列页脚在 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.
推荐答案
我遇到了同样的问题,previously,我意识到一个漫长的寻找之后;
I ran into the same problem previously and after a long search I realised;
Winform的DataGridView的不支持添加脚注吧。
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.
Solution--
Solution--
我的解决方案,它使用的数据绑定。(1)-I创建一个抽象对象的项目与(名称,成本)的特性。(2)-1创建一个具体的项目,即 ConcItem 继承项目(3)-I创建一个页脚项目即 FooterItem 也继承了项目(4)-A集合中的项目,即ITEMLIST你实例页脚项目。(5)最后,你做的数据绑定调用,增加页脚项目的方法之前。
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);
}
}
相关推荐
最新文章