请教各位一个问题,我想对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如何指定呢?

解决方案 »

  1.   

    不用你自己调用,CellFormatting是datagridview的事件,写上就好了,双击事件属性,你在里面添加代码就行了
      

  2.   

      请教各位,上述问题应该怎样解决,我也遇到了类似的问题。我是从数据库中加载数据到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;
                    }
                }
            }
        }
    }
      

  3.   

    有答案吗?我在做控件时也碰到类似问题,我重画oncellpaintting,因没有办法刷新oncellpaintting,出现画出来的单元格叠加残影,能否告之