3

ページが読み込まれると、datagridview がデータベースからバインドされます。

行を選択し、データベースからデータを取得するために行入力イベントが必要です。

ただし、ロード時には発生しないはずです。これどうやってするの ?

これは私のコードです

private void dgStation_RowEnter(object sender, DataGridViewCellEventArgs e)
{
    dgStation.Rows[e.RowIndex].Selected = true;
    int id = Convert.ToInt32(dgStation.Rows[e.RowIndex].Cells[1].Value.ToString());
}
4

1 に答える 1

6

次のように、フォームがロードされた後にワイヤ ハンドラをアタッチできます。

protected override void OnShown(EventArgs e) {
  base.OnShown(e);
  dgStation.RowEnter += dgStation_RowEnter;
}

デザイナー ファイルから現在の RowEnter ハンドラーを必ず削除してください。

または、読み込みフラグを使用します。

private bool loading = true;

protected override void OnShown(EventArgs e) {
  base.OnShown(e);
  loading = false;
}

private void dgStation_RowEnter(object sender, DataGridViewCellEventArgs e) {
  if (!loading) {
    dgStation.Rows[e.RowIndex].Selected = true;
    int id = Convert.ToInt32(dgStation.Rows[e.RowIndex].Cells[1].Value.ToString());
  }
}
于 2012-08-14T12:53:36.930 に答える