Ich bin auf der Suche nach einer Möglichkeit, eine Zelle mit einem Text zu färben, der "X" enthält, nachdem ich Daten aus meiner Datenbank abgerufen habe. Mit dem Code, den ich habe, färbt er die ganze Reihe. Wie mache ich es für die Zellen, die das "X" enthalten?Wie man eine bestimmte Zelle in dataGridView C automatisch einfärbt #
Dies ist der Code, den ich bisher habe:
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["Timee"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["CarColorNumber"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["Interior"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["Exterior"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["CPlastic"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["MPlastic"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["SPlastic"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["PlasticB"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["WashExt"].ToString();
dataGridView1.Rows[n].Cells[9].Value = item["WashEng"].ToString();
dataGridView1.Rows[n].Cells[10].Value = item["WashTrunk"].ToString();
dataGridView1.Rows[n].Cells[11].Value = item["WashSeats"].ToString();
dataGridView1.Rows[n].Cells[12].Value = item["SeatsRmv"].ToString();
dataGridView1.Rows[n].Cells[13].Value = item["SeatsFit"].ToString();
dataGridView1.Rows[n].Cells[14].Value = item["Notes"].ToString();
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[2].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[3].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[3].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[4].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[4].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[5].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[5].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[6].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[6].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[7].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[7].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[8].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[8].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[9].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[9].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[10].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[10].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[11].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[11].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[12].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[12].Style.BackColor = Color.GreenYellow;
}
if (dataGridView1.Rows[i].Cells[13].Value.ToString().Contains(" X"))
{
dataGridView1.CurrentRow.Cells[13].Style.BackColor = Color.GreenYellow;
}
}
es zu tun versuchen im [CellFormatting-Ereignis] (https: //msdn.microso ft.com/en-us/library/system.windows.forms.datagridview.cellformatting(v=vs.110).aspx) – Pikoh
Und als eine Empfehlung würde ich diese 'dataGridView1.Rows [i] .Cells [2 ändern ] .Value.ToString(). Enthält ("X") 'in folgendes:' dataGridView1.Rows [i] .Cells [2] .Value.ToString(). Trim(). Enthält ("X"); ' – Pikoh