C#でDataGridViewに配置したチェックボックスの状態取得


 DataGridView のカラムにチェックボックスを入れて、チェックボックスをクリックしたときにリアルタイムにDataGridViewの同一行等に対してスタイルを変更したり、カラムに書き込んだりする方法がやっとわかった。
 要するにDataGridViewのCurrentCellDirtyStateChangedイベントで、チェックボックスを配置しているカラムのイベントを捉えて、コミットさせて、そのカラムの値を判定してチェックボックスの状態を取得すればいいわけねぇ。

private void Form1_Load(object sender, EventArgs e)
{
  string[] strItem = {"Desktop PC", "Note PC", "Tablet" };

  for (int i = 0 ; i < strItem.Length; i++)
  {
    dataGridView1.Rows.Add();
    dataGridView1["Column2", i].Value = strItem[i];
  }
}
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
  if (dataGridView1.CurrentCellAddress.X == 0 && dataGridView1.IsCurrentCellDirty)
  {
    // — Commit!
    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
  if (dataGridView1["Column1",
dataGridView1.CurrentCellAddress.Y].Value.ToString() == "True")
  {
    for (int i = 0; i < dataGridView1.Columns.Count; i++)
    {
      dataGridView1[i, dataGridView1.CurrentCellAddress.Y].Style.BackColor = Color.Red;
    }
    dataGridView1["Column3", dataGridView1.CurrentCellAddress.Y].Value = "購入";
  }
  else
  {
    for (int i = 0; i < dataGridView1.Columns.Count; i++)
    {
      dataGridView1[i, dataGridView1.CurrentCellAddress.Y].Style.BackColor = Color.White;
    }
    dataGridView1["Column3", dataGridView1.CurrentCellAddress.Y].Value = null;
    }
  }
}

ソフトウェア開発の素直研究所です。お仕事のご依頼はこちらからどうぞ!

“C#でDataGridViewに配置したチェックボックスの状態取得” への4件の返信

  1. Unquestionably believe that which you said. Your favourite justification appeared to be at the internet the easiest factor to take into account of.
    I say to you, I certainly get irked while other folks think about issues that they plainly do not recognize about.

    You controlled to hit the nail upon the top as neatly as outlined out
    the whole thing without having side-effects , other people can take a signal.
    Will probably be again to get more. Thanks! http://www.gaycockboys.com/video/free-brokestraightboys-videos-J9KLzgWmE07.html

free brokestraightboys videos へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。