データベースからデータを取得した後にdataGridViewセルの色を付ける必要があります。セルテキストに「X」が付いている場合は、セルをGreenYellowカラーで色付けします。私はコードを書こうとしたが、うまくいかなかった。C#のテキスト条件でdataGridViewセルの色を変更する方法
これは私がこれまで持っているコードです:
private void button2_Click(object sender, EventArgs e)
{
string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("Select * from TopShineDB.Table1 ;", conDataBase);
using (MySqlConnection conn = new MySqlConnection(constring))
{
try {
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dt = new DataTable();
sda.Fill(dt);
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();
if (dataGridView1.CurrentCell.Value == item["Interior"] + " X".ToString())
{
dataGridView1.CurrentCell.Style.BackColor = Color.GreenYellow;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
すべてのアイデア、私はそれを動作させることができますか?
ありがとうございました
私はコードを編集しました。それは、データベースからデータを取得するためにボタンクリックで機能するはずです。 –
a) 'inter'とは何ですか? b) '' X "'はすでに文字列です! c)現在のセルがあると仮定しないでください!代わりに処理している行の右のセルを変更してください!! – TaW
@TaW申し訳ありませんが、回答のためにコード –