C# 如何仅绘制DataGridView';s单元格背景不是它的内容?

C# 如何仅绘制DataGridView';s单元格背景不是它的内容?,c#,datagridview,C#,Datagridview,我只需要画DataGridView单元格的背景,而不是它的内容。但当我画它的时候,也要画它的内容。请帮助我 我的代码是这样的 private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex == 0 ) { using (Brush gridBrush

我只需要画DataGridView单元格的背景,而不是它的内容。但当我画它的时候,也要画它的内容。请帮助我

我的代码是这样的

private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == 0 )

            {
                using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor))
                {
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // Clear cell 
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                            //Bottom line drawing
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1);


                            e.Handled = true;
                        }
                    }
                }
            }

我不知道为什么你需要捕捉细胞绘画事件来改变细胞背景颜色,就这样做吧

Daywisegrid.Rows[RowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
但如果你想在绘画中做到这一点,试试这个

private void Daywisegrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == 0 )

            {
                using (Brush gridBrush = new SolidBrush(this.Daywisegrid.GridColor))
                {
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        using (Pen gridLinePen = new Pen(gridBrush))
                        {
                            // Clear cell 
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                            //Bottom line drawing
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right, e.CellBounds.Bottom-1);

                              // here you force paint of content
                             e.PaintContent( e.ClipBounds  );
                            e.Handled = true;
                        }
                    }
                }
            }
应该是

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom-1 , e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
e.CellBounds.Right,e.CellBounds.Bottom-1
点将被下一个单元格擦除