2013-07-18 107 views
15

Tôi có đối tượng dataGridView được điền dữ liệu. Tôi muốn nhấn một nút và thay đổi màu nền của ô. Đây là những gì tôi hiện có:Thay đổi màu của ô dữ liệu datagridview động

foreach(DataGridViewRow row in dataGridView1.Rows) 
{ 
    foreach(DataGridViewColumn col in dataGridView1.Columns) 
    { 
      //row.Cells[col.Index].Style.BackColor = Color.Green; //doesn't work 
      //col.Cells[row.Index].Style.BackColor = Color.Green; //doesn't work 
     dataGridView1[col.Index, row.Index].Style.BackColor = Color.Green; //doesn't work 
    } 
} 

TẤT CẢ những thứ này làm cho bảng được vẽ lại theo cách chồng chéo và cố gắng sắp xếp lại các bảng trở thành một mớ hỗn độn. khi nhấp vào một ô, giá trị vẫn được đánh dấu và màu nền không thay đổi.

Hỏi: Làm thế nào tôi có thể thay đổi màu sau của một ô riêng lẻ sau khi bảng tồn tại?

Trả lời

46

này làm việc cho tôi

dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red; 
+1

Add; ở cuối – szakwani

+0

Kính gửi Ehsan, Cảm ơn bạn đã tip nó làm việc cho tôi. – t4thilina

+0

@ t4thilina, Rất vui được. Chúc mừng :) – Ehsan

0

Nhờ nó làm việc

đây tôi đang thực hiện với điều này bằng cách lĩnh vực qty là zero có nghĩa là nó chỉ ra rằng tế bào màu đỏ

 int count = 0; 

     foreach (DataGridViewRow row in ItemDg.Rows) 
     { 
      int qtyEntered = Convert.ToInt16(row.Cells[1].Value); 
      if (qtyEntered <= 0) 
      { 
       ItemDg[0, count].Style.BackColor = Color.Red;//to color the row 
       ItemDg[1, count].Style.BackColor = Color.Red; 

       ItemDg[0, count].ReadOnly = true;//qty should not be enter for 0 inventory      
      } 
      ItemDg[0, count].Value = "0";//assign a default value to quantity enter 
      count++; 
     } 

    } 
2

Thực hiện phần mở rộng của riêng bạn của DataGridViewTextBoxCell và ghi đè phương pháp Paint như sau:

class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell 
{ 
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, 
     DataGridViewElementStates cellState, object value, object formattedValue, string errorText, 
     DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) 
    { 
     if (value != null) 
     { 
      if ((bool) value) 
      { 
       cellStyle.BackColor = Color.LightGreen; 
      } 
      else 
      { 
       cellStyle.BackColor = Color.OrangeRed; 
      } 
     } 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, 
      formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); 
} 

}

Sau đó, trong mã thiết lập thuộc tính CellTemplate cột của bạn để thể hiện của lớp học của bạn

columns.Add(new DataGridViewTextBoxColumn() {CellTemplate = new MyDataGridViewTextBoxCell()}); 
+0

lol, tôi đã chọn màu sắc tốt nhất cho 'cảnh báo' và 'ok' trước đó, và tôi cũng đã chọn 'Color.OrangeRed' cho 'warning' ở cuối, nhưng' Color.SpringGreen' cho 'OK'. – n00dles

Các vấn đề liên quan