2017-10-22 18 views
-1

私は以下のコードを使用してdbから特定のデータを呼び出しています。C#データの内容に応じてdatagridviewの背景色を設定します

private void textBox25_TextChanged(object sender, EventArgs e) 
    { 
     con.Close(); 
     con.Open(); 
     SqlDataAdapter SDA = new SqlDataAdapter("select * from V_CustomerUnits2 where unitmodel like N'%" + textBox25.Text + "%'", con); 
     DataTable DATA = new DataTable(); 
     SDA.Fill(DATA); 
     dataGridView4.Rows.Clear(); 
     foreach (DataRow item in DATA.Rows) 
     { 
      int n = dataGridView4.Rows.Add(); 
      dataGridView4.Rows[n].Cells[0].Value = "false"; 
      dataGridView4.Rows[n].Cells[1].Value = item["fixid"].ToString(); 
      dataGridView4.Rows[n].Cells[2].Value = item["CompanyName"].ToString(); 
      dataGridView4.Rows[n].Cells[3].Value = item["unitmodel"].ToString(); 
      dataGridView4.Rows[n].Cells[4].Value = item["unitserial"].ToString(); 
      dataGridView4.Rows[n].Cells[5].Value = item["manufacturer"].ToString(); 
      dataGridView4.Rows[n].Cells[6].Value = item["problemdesc"].ToString(); 
      dataGridView4.Rows[n].Cells[7].Value = item["technotes"].ToString(); 
      dataGridView4.Rows[n].Cells[8].Value = item["dateaddedunit"].ToString(); 
      dataGridView4.Rows[n].Cells[9].Value = item["datefixed"].ToString(); 
      dataGridView4.Rows[n].Cells[10].Value = item["total"].ToString(); 
      dataGridView4.Rows[n].Cells[11].Value = item["invoicestatus"].ToString(); 
      dataGridView4.Rows[n].Cells[12].Value = item["invoicen"].ToString(); 
      Countrecords.Enabled = true; 
     } 
    } 

特定の結果に基づいて各行の裏面の色が必要です。

たとえば、ユニットが修理されている場合は、オレンジ色に修正できない場合は赤色に修復されないと緑色になります。

助けが必要ですか?

+0

ザッツはSQLを構築するための正しい方法ではありません、それはDGVを移入するための最良の方法は程遠いです...あなたは、クエリ、彼らがキーを押すたびに実行されていることを気にしません。 – Plutonix

+0

私はこのコードを成功せずに試しました – MicrosiM

答えて

0

行のDefaultCellStyle.BackColorプロパティを必要以上に使用します。例:

dataGridView4.Rows[n].DefaultCellStyle.BackColor = Color.Green; 
+0

@ Plutonix、SQlをビルドする最良の方法、または上記の異なるSQLを実行する例? – MicrosiM

関連する問題