2014-02-24 189 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

我在哪裏檢查? –

相關問題