![](/img/trans.png)
[英]How to add a value in the cells of a specific column in a datagridview in c#.net?
[英]DataGridView column footer c#.net winforms
有沒有辦法在非datagridview
綁定的datagridview
添加列頁腳? 我正在使用它來獲取用戶輸入以添加庫存。 目前我正在使用標簽來顯示總數,但如果可能,我想將其更改為頁腳。
我之前遇到過同樣的問題,經過長時間的搜索,我意識到;
Winform Datagridview 不支持向其添加頁腳。
我嘗試添加一個可以保存摘要的額外行,但仍然無法正常工作。
您可以創建一個用戶控件,它有兩個網格,下面的網格包含摘要。
解決方案 -
我使用數據綁定的解決方案。(1)-我創建了一個具有(名稱,成本)屬性的抽象對象項。(2)-我創建了一個具體項,即繼承項的ConcItem (3)-我創建了一個頁腳項,即FooterItem它還繼承了Item (4) - Items 的集合,即 ItemList,您可以在其中實例化頁腳項。(5) 最后,就在進行數據綁定之前,調用添加頁腳項的方法。
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 的 NewRow 解決了這個問題。
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<DataGridViewRow>().Where(a => a.Index != a.DataGridView.NewRowIndex).Select(a => (double)a.Cells[2].Value).Sum().ToString("N2");
}
}
}
}
}
這是它如何工作的實時屏幕截圖。
有一個更好的解決方案。 在這個解決方案中,每次單元格內容發生變化時,都會重新計算並顯示該值。
我的數據網格視圖如下所示。
在此解決方案中,您可以擁有任意數量的列和任意數量的行。
private void updateDataGridViewTotal(DataGridView dgv)
{
DataTable dt = (DataTable)dgv.DataSource;
int lastRow = (dgv.Rows.Count - 1);
if (dt.Rows[lastRow][0].ToString() == "Total")
{
dt.Rows.RemoveAt(lastRow);
}
int[] tot = new int[dt.Columns.Count];
Array.Clear(tot, 0, tot.Length);
foreach (DataRow row in dt.Rows)
{
int rowSum = 0;
for (int i = 1; i < dt.Columns.Count - 1; i++)
{
tot[i] += Convert.ToInt32(row[i]);
rowSum += Convert.ToInt32(row[i]);
}
row["Total"] = rowSum;
}
DataRow newRow = dt.NewRow();
newRow["Agent"] = "Total";
for (int i = 1; i < tot.Length; i++)
{
newRow[i] = tot[i];
}
dt.Rows.Add(newRow);
dgv.DataSource = dt;
}
只需將您的 datagridview 傳遞給該函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.