C# DataGridView合计行

suenjoe 2012-05-08 02:09:58
我是这样实现的,通过一个datagridview1.controls.add(),这个方法把另外一个datagridview2添加到现有的datagridview1中去,然后设置新添加的那个,但是有个问题就是滚动条拖动的时候下面的datagridview2总是挡住了几条数据,并且datagridview不能实现添加编辑,不能编辑的情况下可以通过添加空白行把数据显示完全,但是如果想要添加编辑就没办法了,不求解决,只求高手能够提供思路..
...全文
341 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
请叫我卷福 2012-05-08
  • 打赏
  • 举报
回复
注意不要漏了e.Handled=true; 否则白费了 它继续按照自己的方法绘制datagridview的网格
请叫我卷福 2012-05-08
  • 打赏
  • 举报
回复
监听datagridview的CellPainting事件,在事件处理程序中添加如下代码:(横向合并与纵向合并)

//绘制单元格
private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
try
{

////纵向合并
//if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
//{

// 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);
// /**/////绘制线条,这些线条是单元格相互间隔的区分线条,
// ////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条
// if (e.RowIndex != this.dataGridView1.RowCount - 1)
// {
// if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
// {

// e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
// e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
// //绘制值
// if (e.Value != null)
// {
// e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
// Brushes.Crimson, e.CellBounds.X + 2,
// e.CellBounds.Y + 2, StringFormat.GenericDefault);
// }
// }
// }
// else
// {
// e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
// e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
// //绘制值
// if (e.Value != null)
// {
// e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
// Brushes.Crimson, e.CellBounds.X + 2,
// e.CellBounds.Y + 2, StringFormat.GenericDefault);
// }
// }
// //右侧的线
// e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
// e.CellBounds.Top, e.CellBounds.Right - 1,
// e.CellBounds.Bottom - 1);

// e.Handled = true;
// }
// }
//}

//横向合并
if (e.ColumnIndex != 0 && e.RowIndex == this.dataGridView1.Rows.Count - 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 (this.dataGridView1.Columns["StuName"].Index == e.ColumnIndex || this.dataGridView1.Columns["SubjectNo"].Index == e.ColumnIndex || this.dataGridView1.Columns["SubjectScore"].Index == e.ColumnIndex
|| this.dataGridView1.Columns["SubjectTime"].Index == e.ColumnIndex)
{
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
}
//绘制值
if (e.Value != null)
{
e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
Brushes.Black, e.CellBounds.X + 2,
e.CellBounds.Y + 5, StringFormat.GenericDefault);
}


//下边缘的线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.Handled = true;
}
}

}
}
catch (Exception ex)
{

}

}
bdmh 2012-05-08
  • 打赏
  • 举报
回复
搞个panel,上下各方一个gridview不好吗

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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