关于VS2005 WinForm下DataGridView单元格合并,背景色设置

最近发现这方面在社区内提问的不少,发个源代码,嘿嘿

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("A");
            dt.Columns.Add("B");
            dt.Columns.Add("C");
            dt.Columns.Add("D");
            dt.Rows.Add(new object[] { "A1", "B1", "C1", "D1" });
            dt.Rows.Add(new object[] { "A2", "B2", "C2", "D2" });
            dt.Rows.Add(new object[] { "A3", "B3", "C3", "D3" });
            this.dataGridView1.DataSource = dt;

        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

            if (e.RowIndex < 0 || e.RowIndex >= this.dataGridView1.Rows.Count - 1 || e.ColumnIndex == -1)
            {
                return;
            }

            // 假定需要将C列值为C3的单元格与前一单元格合并
            if (e.ColumnIndex == 1 && this.dataGridView1[e.RowIndex, e.ColumnIndex + 1].Value.ToString() == "C3")
            {
                e.Handled = true;
            }
            if (e.ColumnIndex == 2 &&  e.Value.ToString() == "C3")
            {
                DataGridViewCell preCell = this.dataGridView1[e.ColumnIndex -1, e.RowIndex];
                Rectangle re = new Rectangle(e.CellBounds.Left - this.dataGridView1.Columns[e.ColumnIndex - 1].Width
                    , e.CellBounds.Top, e.CellBounds.Width + this.dataGridView1.Columns[e.ColumnIndex - 1].Width, e.CellBounds.Height);
                e.Graphics.FillRectangle(Brushes.White,  re);

                Pen pen = new Pen(this.dataGridView1.BackgroundColor,1);
                pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                e.Graphics.DrawLine(pen, re.X, re.Y + re.Height-1, re.X + re.Width, re.Y + re.Height-1);
                e.Graphics.DrawLine(pen, re.X + re.Width -1, re.Y , re.X + re.Width -1, re.Y + re.Height);

                SizeF strSize = e.Graphics.MeasureString(e.Value.ToString(), this.dataGridView1.Font);
                e.Graphics.DrawString(e.Value.ToString(), this.dataGridView1.Font
                    , Brushes.Black, re.X, re.Y + (re.Height - strSize.Height) / 2);

                e.Handled = true;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if(e.RowIndex != -1 && e.ColumnIndex == 1 && e.Value != null && e.Value.ToString() == "B1")
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.ForeColor = Color.Blue;
                e.CellStyle.SelectionBackColor = Color.BlueViolet;
            }
        }

    } 

 
在Windows Forms的DataGridView中,没有直接提供合并单元格的功能。但是可以通过在CellPainting事件中使用Graphics.DrawLine和Graphics.DrawString方法来手动实现合并单元格的效果。 下面是一个示例代码,演示如何在DataGridView合并单元格: ```csharp private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { DataGridView dgv = (DataGridView)sender; DataGridViewCell cell1 = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; DataGridViewCell cell2 = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex + 1]; // 判断两个相邻单元格的值是否相同,如果相同则合并单元格 if (cell1.Value != null && cell2.Value != null && cell1.Value.ToString() == cell2.Value.ToString()) { e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None; cell2.Style.BackColor = cell1.Style.BackColor; cell2.Style.ForeColor = cell1.Style.ForeColor; // 绘制合并单元格的边框 e.Graphics.DrawLine(Pens.Black, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); // 绘制合并单元格的值 if (e.Value != null) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 2); } e.Handled = true; } } } ``` 在上述代码中,我们通过判断相邻单元格的值是否相同来决定是否合并单元格。如果相同,则将右边单元格的边框设置为无边框,并将背景色和前景色设置为与左边单元格相同。然后使用Graphics.DrawLine方法绘制合并单元格的边框,并使用Graphics.DrawString方法绘制合并单元格的值。 请注意,上述代码只是一个示例,具体的实现方式可能会根据实际需求有所不同。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值