2016-06-23 45 views
0

データベースからデータを取得した後、 "X"を含むテキストでセルをカラーリングする方法をお探しです。私はそれが行全体を色付けしているコードで。どのように私は "X"を含む細胞のためにそれを作るのですか?dataGridViewで特定のセルを自動的にカラーリングする方法C#

これは私がこれまで持っているコードです:

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; 
    } 
}     
+0

それをやって試してみてください[CellFormattingイベント](https://msdn.microso ft://www.windows.jp/library/system.windows.forms.datagridview.cellformatting(v=vs.110).aspx) – Pikoh

+0

アドバイスとして、この 'dataGridView1.Rows [i] .Cells [2 DataGridView1.Rows [i] .Cells [2] .Value.ToString()。Trim()。Contains( "X"); '.Value.ToString()。 – Pikoh

答えて

0

はこれを試してみてください。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (e.Value != null) 
     { 
      if (e.Value.ToString().Equals("X")) 
      { 
       e.CellStyle.BackColor = Color.Red; 
      } 
     } 
    } 

希望に役立ち、

0

あなたはこのように行う必要があります。

1 - あなたのコードを削除します

for (int i = 0; i < dataGridView1.Rows.Count; i++) 
{ 
... 
} 

2 - このイベントを追加

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     try 
     { 
      if ((e.RowIndex > -1 && e.ColumnIndex >-1)) 
      { 
       if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Contains(" X")) 
         e.CellStyle.BackColor = Color.GreenYellow; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message.ToString()); 
     } 
    } 
関連する問題