ひとりも見捨てないことを、あきらめない

学校教育、社会教育、数学、技術家庭科、Youtube、EdTech、ICT、プログラミング、その他

DataGridView インデックスの書き方 04/22 水

Datatable や DataGridView は、Rows と Columns がたくさん出てくるので、書き方が面倒になります。しかし、インデックスを利用した書き方もあるので、メモしておきます。

void myCellClick(object sender, DataGridViewCellEventArgs e)
{
  int nowx, nowy;
  nowx = e.ColumnIndex;
  nowy = e.RowIndex;

  string s = "";
  if ((nowx >= 0) && (nowy >= 0))
  {
     s = Convert.ToString(Dgv[nowx, nowy].Value);
  }

  LB1.Text = "(" + (nowx).ToString() +  "," + (nowy).ToString() + "):" + s;

  for (int i=0; i<4; i++)
  {
    Dgv[i, nowy].Style.BackColor = Color.Yellow;
    Dgv[i, nowy].Style.SelectionBackColor = Color.Yellow;
  }
}

row と column がゴチャゴチャしてしまうのは、いつものことです。何度も、何度も、自分で確認しなくければならないです。

f:id:takase_hiroyuki:20200420160929p:plain