datagirdview 的 cellpainting事件如何触发

jimyang2008 2012-05-30 09:02:07
请教各位一个问题,我想对datagirdview进行单元格合并,网上有很多代码,大多数都利用dataGridView1_CellPainting来实现。


例如下面的代码:
private void dataGridView1_CellFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns["description"].Index == e.ColumnIndex && e.RowIndex >= 0)
{
if (this.nextcol != null & e.ColumnIndex == this.nextcol)
{
e.CellStyle.BackColor = Color.LightBlue;
this.nextcol = null;
}
if (this.nextrow != null & e.RowIndex == nextrow)
{
e.CellStyle.BackColor = Color.LightPink;
this.nextrow = null;
}
if (e.RowIndex != this.dataGridView1.RowCount - 1)
{
if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
{
e.CellStyle.BackColor = Color.LightPink;
nextrow = e.RowIndex + 1;
}
}

但是如何调用该事件,或者说该事件如何触发呢?有的说是在datagirdview添加数据的时候自动触发,可是我试过了,并没有触发啊?如果是调用,那么 这个 sender和参数e如何指定呢?
...全文
1053 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenhu420 2013-08-03
  • 打赏
  • 举报
回复
有答案吗?我在做控件时也碰到类似问题,我重画oncellpaintting,因没有办法刷新oncellpaintting,出现画出来的单元格叠加残影,能否告之
Yf1234121226 2013-02-21
  • 打赏
  • 举报
回复
请教各位,上述问题应该怎样解决,我也遇到了类似的问题。我是从数据库中加载数据到GridView控件上,同时实现表格合并,但是加载数据没问题,但是合并表格的事件并没有触发。代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using System.Data.SqlClient; namespace test28 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { LoadData(); } private void LoadData() { string connStr = "server=localhost;Trusted_Connection=yes;database=test1"; string commStr = "select * from test1"; SqlDataAdapter dataAdapter = new SqlDataAdapter(commStr, connStr); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet, "test1"); DataTable dataTable = new DataTable(); dataTable = dataSet.Tables[0]; this.dataGridView1.DataSource = dataTable; } private void dataGridView1_CellPainting(object sender, System.Windows.Forms.DataGridViewCellPaintingEventArgs e) { //对第一列单元格进行合并 if(e.ColumnIndex == 1 && 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); //画Grid边线(仅画单元格的底边线和右边线) //如果下一行和当前行的数据不同,则在当前的单元格画一条底边线 if( e.RowIndex < dataGridView1.Rows.Count-2 && 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; } } } } }
tangeren2004 2012-11-08
  • 打赏
  • 举报
回复
哥们 这个问题解决没 该怎么弄?
请叫我卷福 2012-05-30
  • 打赏
  • 举报
回复
在单元格需要重新绘制的时候触发 只需要你写事件处理程序 不需要你手动触发
bdmh 2012-05-30
  • 打赏
  • 举报
回复 1
不用你自己调用,CellFormatting是datagridview的事件,写上就好了,双击事件属性,你在里面添加代码就行了

110,546

社区成员

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

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

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