private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)//绘制单元格事件
        {
            // 对第1列相同单元格进行合并     
            if(e.ColumnIndex == 2 && e.RowIndex != -1 || e.ColumnIndex == 3 && e.RowIndex != -1 || e.ColumnIndex == 4 && e.RowIndex != -1) {
                Brush datagridBrush = new SolidBrush(dataGridView1.GridColor);
                SolidBrush groupLineBrush = new SolidBrush(e.CellStyle.BackColor);
                using(Pen datagridLinePen = new Pen(datagridBrush)) {
                    // 清除单元格
                    e.Graphics.FillRectangle(groupLineBrush, e.CellBounds);
                    if(e.RowIndex < dataGridView1.Rows.Count - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) {
                        //绘制底边线
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                        // 画右边线
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                    }
                    else {
                        // 画右边线
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                    }
                    //对最后一条记录只画底边线
                    if(e.RowIndex == dataGridView1.Rows.Count - 1) {
                        //绘制底边线
                        e.Graphics.DrawLine(datagridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                    }
                    // 填写单元格内容,相同的内容的单元格只填写第一个                        
                    if(e.Value != null) {
                        if(!(e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())) {
                            //绘制单元格内容
                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
                        }
                    }
                    e.Handled = true;
                }
            }
        }
//我唯想不通的是,在绘制直线的时候的"减一"的操作.直接在原来的基础上,什么都不减,也不加,不行么?

解决方案 »

  1.   

    嗯...这个我试过...我想是,因为每个单元格画的时候都是在原来的基础上画,如果不减去一个,那么原来的值有些都已经被冲掉....
    现在我自己写的,魔方他做的思路.怎么我做的,最后一行多了个这个出来:也就是说,没有数据了,也画矩形出来,我都找不到自己错在哪...
    if(e.RowIndex != -1 && e.ColumnIndex > 1) {
                    SolidBrush gridPen = new SolidBrush(dataGV.GridColor);  //网格颜色.
                    SolidBrush groupPen = new SolidBrush(e.CellStyle.BackColor);   //合并单元格的填充色.
                    using(Pen p = new Pen(gridPen)) {   //用Pen画线.
                        e.Graphics.FillRectangle(groupPen, e.CellBounds);
                        if(e.RowIndex < dataGV.Rows.Count - 1 && dataGV.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGV.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString()) {
                            //单元格值不同时不合并(即加底线).
                            e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);//这里的减1操作是为了不使继续下一个操作时冲掉之前的网格.
                            e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);  //画右线.                    }
                        else {
                            e.Graphics.DrawLine(p, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);  //画右线.
                        }
                        if(e.RowIndex == dataGV.Rows.Count - 1) {   //最后一个单元格.
                            e.Graphics.DrawLine(p, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);    //画底线.
                        }
                        if(e.Value != null) {
                            if(!(e.RowIndex > 0 && dataGV.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())) {  //相同值的相邻单元格.
                                e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X, e.CellBounds.Y,StringFormat.GenericDefault);
                            }
                        }
                    }
                    e.Handled = true;
                }