2014-02-24 117 views
0

我在C#Windows应用程序中有一个数据网格视图。 我需要改变单元格中最后5个字符的颜色,但我不知道如何去做。在单个DataGridView中设置两种颜色文本单元格

我在CellPainting事件这个代码,但不工作:

private void dgvSorteados_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 
      int sector = 0; 
      int.TryParse(dgvSorteados.Rows[e.RowIndex].Cells[0].Value.ToString(), out sector); 
      if (sector == 3 && rdbSenete3.Checked) 
      { 
       if (dgvSorteados.Columns[1].Index == e.ColumnIndex && e.RowIndex >= 0) 
       { 
        string bolillas = (String)e.Value; 
        string[] bolillasArray = bolillas.Split('-'); 
        string bolillasMin = string.Join("-", bolillasArray.Take(12)); 
        string bolillasResto = string.Join("-", bolillasArray.Skip(12)); 

        using (Brush gridBrush = new SolidBrush(dgvSorteados.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor)) 
        { 
         // Erase the cell. 
         e.Graphics.FillRectangle(backColorBrush, e.CellBounds); 
         // Draw the text content of the cell, ignoring alignment. 
         e.Graphics.DrawString((String)bolillasMin, e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault); 
         if (!string.IsNullOrEmpty(bolillasResto)) 
         { 
          e.Graphics.DrawString("-" + (String)bolillasResto, e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2 + bolillasMin.Length, e.CellBounds.Y + 2, StringFormat.GenericDefault); 
         } 
         e.Handled = true; 
        } 
       } 
      } 
     } 

这段代码显示无行的DataGridView。

回答

1

您可以使用e.PaintBackground调用来避免背景涂色代码。此外,只有在ContentForeGround正在绘制时,您才必须绘制字符串。使用e.PaintParts来识别绘画操作。请参阅我的示例代码了解其用法。它需要调整,但你会得到一个想法。

示例代码:

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    if (e.RowIndex != -1 && e.Value != null && e.Value.ToString().Length > 5 && e.ColumnIndex == InterestedColumnIndex) 
    { 
     if (!e.Handled) 
     { 
      e.Handled = true; 
      e.PaintBackground(e.CellBounds, dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected); 
     } 
     if ((e.PaintParts & DataGridViewPaintParts.ContentForeground) != DataGridViewPaintParts.None) 
     { 
      string text = e.Value.ToString(); 
      string textPart1 = text.Substring(0, text.Length - 5); 
      string textPart2 = text.Substring(text.Length - 5, 5); 
      Size fullsize = TextRenderer.MeasureText(text,e.CellStyle.Font); 
      Size size1 = TextRenderer.MeasureText(textPart1, e.CellStyle.Font); 
      Size size2 = TextRenderer.MeasureText(textPart2, e.CellStyle.Font); 
      Rectangle rect1 = new Rectangle(e.CellBounds.Location, e.CellBounds.Size); 
      using (Brush cellForeBrush = new SolidBrush(e.CellStyle.ForeColor)) 
      { 
       e.Graphics.DrawString(textPart1, e.CellStyle.Font, cellForeBrush, rect1); 
      } 
      rect1.X += (fullsize.Width - size2.Width); 
      rect1.Width = e.CellBounds.Width;      
      e.Graphics.DrawString(textPart2, e.CellStyle.Font, Brushes.Crimson, rect1); 
     } 
    } 
} 
+0

这就是我想要的,但现在的问题是,在DataGridView显示所有深灰色,如果我上出现一排点击。 –

+0

@Phoenix_uy检查您是否设置了与网格背景颜色相同的前景色 – Junaith

+0

我在哪里检查? –

相关问题