using System;
using System.Windows.Forms;
namespace DataGridViewSampCs
{
public class FormHelper
{
#region 添加一行
public void AddRow(DataGridView dg, string value)
{
if (dg.Rows.Count > 1)
{
DelRow(dg, dg.Rows.Count - 1);
}
dg.Rows.Add();
for (int i = 0; i < dg.Columns.Count; i++)
{
dg.Rows[dg.Rows.Count - 1].Cells[i].Value = value;
}
dg.CurrentCell = dg.Rows[dg.Rows.Count - 1].Cells[0];
TotalRow(dg);
}
#endregion 添加一行
#region 编辑一行
public void EditRow(DataGridView dg)
{
DelRow(dg, dg.Rows.Count - 1);
TotalRow(dg);
}
#endregion 编辑一行
#region 删除一行
public void DelRow(DataGridView dg, int index)
{
dg.Rows.Remove(dg.Rows[index]);
}
#endregion 删除一行
#region 合计行
public void TotalRow(DataGridView dg)
{
dg.Rows.Add();
DataGridViewRow dgr = dg.Rows[dg.Rows.Count - 1];
dgr.ReadOnly = true;
dgr.DefaultCellStyle.BackColor = System.Drawing.Color.Khaki;
dgr.Cells[0].Value = "合计";
for (int i = 0; i < dg.Rows.Count - 1; i++)
{
dgr.Cells[3].Value = Convert.ToSingle(dgr.Cells[3].Value) + Convert.ToSingle(dg.Rows[i].Cells[3].Value);
}
}
#endregion 合计行
}
}
转载于:https://www.cnblogs.com/liulf/archive/2011/02/24/1963484.html