2011-05-18 5 views
5

necesito pintar solo el fondo de la celda DataGridView, no su contenido. Pero mientras estoy pintando, pinta su contenido también. Por favor, ayúdenme.¿Cómo pintar solo el fondo de celda de DataGridView no su contenido?

Mi código es así.

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; 
         } 
        } 
       } 
      } 

Respuesta

8


no estoy seguro de por qué es necesario coger caso CellPainting para cambiar el color de fondo de células sólo lo hacen como esto

Daywisegrid.Rows[RowIndex].Cells[columnIndex].Style.BackColor = Color.Red; 

Pero si quieres hacerlo en la pintura prueba este

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; 
         } 
        } 
       } 
      } 
0

En caso de ser

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 punto sería borrado por la siguiente celda.

Cuestiones relacionadas