Chciałbym zapytać, w jaki sposób wiersz automatycznie aktualizuje bazę kolorów swojej czcionki na wartość kolumny w dataGridView
.(datagridview) aktualizuje bazę kolorów wiersza w wierszu o wartości kolumny
Na przykład tabela ma 4 kolumny: id, name, rentPayMent and check
.
Sprawdź każdym wierszu, aby zobaczyć, czy któryś z wartością check == 0
Jeśli tak, to czcionki ten wiersz za color = red
Else do nothing
w ruchu, i użyć kodu w następujący sposób, ale to wydobyć błąd z
obiekt odniesienia nie jest ustawiony na przykład przedmiotu, była System.NullReferenceException nieobsłużonego
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[3].Value.ToString() == "0") //**Object reference not set to an instance of an object**
{
row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red
}
}
}
Dzięki za wszystko, mam już rozwiązanie.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[3].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString()))
{
if (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString().Trim() == "0")
dataGridView1.Rows[e.RowIndex].DefaultCellStyle = new DataGridViewCellStyle { ForeColor = Color.Red };
}
else
{
dataGridView1.Rows[e.RowIndex].Cells[3].Style = dataGridView1.DefaultCellStyle;
}
}
myślę, że należy Refar ten link Moje Bądź Pomoc Jesteś [staskoverflow] [1] [1]: http://stackoverflow.com/questions/12202751/how-can-i-make-a-datagridview-cells-font-a-particular-color –
winforms lub asp.net? –
jest w WinForm. – Kam2012