Winform怎样添加统计行并合并单元格

yth126 2011-01-11 02:55:42
如下图:

我要的是Winform的,不是Web版
...全文
100 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2011-01-11
  • 打赏
  • 举报
回复
合并
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
using
(
Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor)
)
{
using (Pen gridLinePen = new Pen(gridBrush))
{
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
if (e.RowIndex < dataGridView1.Rows.Count - 1 &&
dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() !=
e.Value.ToString())
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
if (e.Value != null)
{
if (e.RowIndex > 0 &&
dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() ==
e.Value.ToString())
{ }
else
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Black, e.CellBounds.X + 2,
e.CellBounds.Y + 5, StringFormat.GenericDefault);
}
}
e.Handled = true;
}
}
}
http://topic.csdn.net/u/20100609/13/4d9006e4-529e-4b30-91ae-9908f5806c58.html
纯唇Yu弄 2011-01-11
  • 打赏
  • 举报
回复

//把下面
//#region 添加DataGridView底行合计数 的四个方法 和 #endregion 添加DataGridView底行合计数 的四//个方法之间的 代码拷贝到你的程序中

//然后再 给要计算合计的 DataGridView 对象(此处假定为dataGridView1) 绑定 下面的方法及 数据表 则可:

//这里假设通过点击 按钮 之后 绑定

private void button1_Click(object sender, EventArgs e)
{

dataGridView1.DataSourceChanged += new EventHandler(dataGridView_DataSourceChanged);
dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView_ColumnHeaderMouseClick);
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView_CellValueChanged);
dataGridView1.AllowUserToAddRows = false;
dataGridView1.DataSource = 这里是要绑定的数据表;

}



#region 添加DataGridView底行合计数 的四个方法
/// <summary>
/// 计算合计算
/// </summary>
/// <param >要计算的DataGridView</param>
private void SumDataGridView(DataGridView dgv)
{

#region 计算合计数
//DataGridView dgv = (DataGridView)sender;
if (dgv.DataSource == null) return;
DataTable dt = (DataTable)dgv.DataSource;
if (dt.Rows.Count < 1) return;
decimal[] tal = new decimal[dt.Columns.Count];

DataRow ndr = dt.NewRow();

string talc = "";

int number = 1;
foreach (DataRow dr in dt.Rows)
{
dr["@xu.Hao"] = number++;
int n = 0;
foreach (DataColumn dc in dt.Columns)
{


if (talc == "" && dc.DataType.Name.ToUpper().IndexOf("STRING") >= 0) talc = dc.ColumnName;


if (dc.DataType.IsValueType)
{
string hej = dr[dc.ColumnName].ToString();
try
{
if (hej != string.Empty) tal[n] += decimal.Parse(hej);
}
catch (Exception) { }
//if (hej != string.Empty) tal[n] += decimal.Parse(hej);
}


n++;
}
}

ndr.BeginEdit();
for (int i = 0; i < dt.Columns.Count; i++)
{
if (tal[i] != 0)
ndr[i] = tal[i];
}
ndr["@xu.Hao"] = ((int)(dt.Rows.Count + 1)).ToString();
if (talc != "") ndr[talc] = "[合计]";
ndr.EndEdit();
dt.Rows.Add(ndr);

dgv.Rows[dgv.Rows.Count - 1].DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 210);


if (dgv.Tag == null)
{
foreach (DataGridViewColumn dgvc in dgv.Columns)
{
dgvc.SortMode = DataGridViewColumnSortMode.Programmatic;
}
}


dgv.Tag = ndr;

#endregion


}
private void dataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{//
//直接拷贝代码

DataGridView sortDgv = (DataGridView)sender;
int fx = 0;
if (sortDgv.AccessibleDescription == null)
{
fx = 1;
}
else
{
fx = int.Parse(sortDgv.AccessibleDescription);
fx = (fx == 0 ? 1 : 0);
}
sortDgv.AccessibleDescription = fx.ToString();
if (sortDgv.Columns[e.ColumnIndex].Name == "@xu.Hao") return;
DataGridViewColumn nCol = sortDgv.Columns[e.ColumnIndex];

if (nCol.DataPropertyName == string.Empty) return;

if (nCol != null)
{
sortDgv.Sort(nCol, fx == 0 ? ListSortDirection.Ascending : ListSortDirection.Descending);

}
//--
DataRow dr = (DataRow)sortDgv.Tag;
DataTable dt = (DataTable)sortDgv.DataSource;
DataRow ndr = dt.NewRow();
ndr.BeginEdit();
for (int i = 0; i < dt.Columns.Count; i++)
{
ndr[i] = dr[i];
}
dt.Rows.Remove(dr);


//if (e.ColumnIndex != 0)
{
int n = 1;
for (int i = 0; i < sortDgv.Rows.Count; i++)
{
DataGridViewRow dgRow = sortDgv.Rows[i];
DataRowView drv = (DataRowView)dgRow.DataBoundItem;
DataRow tdr = drv.Row;
tdr.BeginEdit();
tdr["@xu.Hao"] = n;
n++;
tdr.EndEdit();

}
sortDgv.Refresh();
sortDgv.RefreshEdit();

}
ndr["@xu.Hao"] = ((int)(dt.Rows.Count + 1)).ToString();
ndr.EndEdit();
dt.Rows.Add(ndr);
sortDgv.Tag = ndr;

//--
sortDgv.Sort(sortDgv.Columns["@xu.Hao"], ListSortDirection.Ascending);
sortDgv.Columns["@xu.Hao"].HeaderCell.SortGlyphDirection = SortOrder.None;
nCol.HeaderCell.SortGlyphDirection = fx == 0 ? SortOrder.Ascending : SortOrder.Descending;
sortDgv.Rows[sortDgv.Rows.Count - 1].DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 210);

}
private void dataGridView_DataSourceChanged(object sender, EventArgs e)
{
DataGridView dgv = (DataGridView)sender;
DataTable dt = (DataTable)dgv.DataSource;
if (dt == null) return;
decimal[] tal = new decimal[dt.Columns.Count];
if (dt.Columns.IndexOf("@xu.Hao") < 0)
{
DataColumn dc = new DataColumn("@xu.Hao", System.Type.GetType("System.Int32"));
dt.Columns.Add(dc);
dgv.Columns["@xu.Hao"].DisplayIndex = 0;
dgv.Columns["@xu.Hao"].HeaderText = "#";

dgv.Columns["@xu.Hao"].SortMode = DataGridViewColumnSortMode.Programmatic;
dgv.AutoResizeColumn(dgv.Columns["@xu.Hao"].Index); 

dgv.Columns["@xu.Hao"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
}
SumDataGridView(dgv);
}
private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{//
DataGridView dgv = (DataGridView)sender;
if (dgv.Tag == null || e.RowIndex < 0 || e.RowIndex == dgv.Rows.Count - 1) return;

string col = dgv.Columns[e.ColumnIndex].DataPropertyName;
if (col == string.Empty) return;
if (((DataRowView)dgv.Rows[e.RowIndex].DataBoundItem).Row.Table.Columns[col].DataType.IsValueType)
{
decimal tal = 0;
foreach (DataGridViewRow dgvr in dgv.Rows)
{
if (dgvr.Index != dgv.Rows.Count - 1)
{
string hej = dgvr.Cells[e.ColumnIndex].Value.ToString();
if (hej != string.Empty) tal += decimal.Parse(hej);
}
}
if (tal == 0)
dgv[e.ColumnIndex, dgv.Rows.Count - 1].Value = DBNull.Value;
else
dgv[e.ColumnIndex, dgv.Rows.Count - 1].Value = tal;
}
}
#endregion 添加DataGridView底行合计数 的四个方法
yth126 2011-01-11
  • 打赏
  • 举报
回复
为什么不能显示图片?
yth126 2011-01-11
  • 打赏
  • 举报
回复
[code=HTML]
code]

62,242

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧