Life is Really Short, Have Your Life!!

ござ先輩の主に技術的なメモ

c#でDataGridViewの背景色が変わらなかった件

フォームのLoadメソッドで、DataGridViewのデータソースにDataTableを入れてから、とある条件にそって背景色を変えるメソッドを実装した。

     Regex reg = new Regex("正規表現");
     foreach (DataGridViewRow row in DataGridView1.Rows)
     {
        if (reg.IsMatch(row.Cells["name"].Value.ToString()))
        {
              row.DefaultCellStyle.BackColor = Color.Yellow;
        }
     }

うごかねー。かわらねー。なんでやろー。

困ったので、適当にPaintイベントをオーバーライドしてもっかい描画したら色が変わった。

 DataGridView1.Paint += DataGridView1_Paint;
 void DataGridView1_Paint(object sender, PaintEventArgs e)
        {
            Regex reg = new Regex("正規表現");
            foreach (DataGridViewRow row in DataGridView1.Rows)
            {
                if (reg.IsMatch(row.Cells["name"].Value.ToString()))
                {
                    row.DefaultCellStyle.BackColor = Color.Yellow;
                }
            }
        }

うーん・・・・。ま、いいか。