2017-06-20 12 views
1

私はDataGridViewwhereを持っています。私はAccessデータベースから情報を取り込みます。私のネットワークからのIPとしてDataGridViewの最初の列。私がしようとしているのは、キーボードから矢印を上下に使い、各行の情報をTextBoxに表示することです。これは、コードである:C#のDataGridViewで矢印を使用

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
{ 
    bool pingable = false; 
    Ping pinger = new Ping(); 
    foreach (DataGridViewRow row in dataGridView1.SelectedRows) 
    { 
     PingReply reply = pinger.Send(row.Cells[0].Value.ToString()); 
     if (e.KeyCode == Keys.Down) 
     { 
      txtIP.Text = row.Cells[0].Value.ToString(); 
      txtUser.Text = row.Cells[1].Value.ToString(); 
      txtComputer.Text = row.Cells[2].Value.ToString(); 
      txtUnity.Text = row.Cells[3].Value.ToString(); 
      txtSession.Text = row.Cells[4].Value.ToString(); 
      if (pingable = reply.Status == IPStatus.Success) 
      { 
       pictureBoxGreen.Show(); 
       pictureBoxRed.Hide(); 
      } 
      else if (pingable = reply.Status == IPStatus.TimedOut) 
      { 
       pcGreen.Hide(); 
       pcRed.Show(); 
      } 
     } 
     if (e.KeyCode == Keys.Up) 
     { 
      txtIP.Text = row.Cells[0].Value.ToString(); 
      txtUser.Text = row.Cells[1].Value.ToString(); 
      txtComputer.Text = row.Cells[2].Value.ToString(); 
      txtUnity.Text = row.Cells[3].Value.ToString(); 
      txtSession.Text = row.Cells[4].Value.ToString(); 
      if (pingable = reply.Status == IPStatus.Success) 
      { 
       pictureBoxGreen.Show(); 
       pictureBoxRed.Hide(); 
      } 
      else if (pingable = reply.Status == IPStatus.TimedOut) 
      { 
       pictureBoxGreen.Hide(); 
       pictureBoxRed.Show(); 
      } 
     } 
    } 
} 

問題はDataGridViewの最初の行に、それが適切な情報を表示し、代わりに、上の行からの情報は表示されません矢印を使用し、例えばクリックした後、です。何が問題なのか知っていますか?

答えて

1
private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
{     
    if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) 
    { 
     var index = e.KeyCode == Keys.Up ? -1 : e.KeyCode == Keys.Down ? 1 : 0; 
     var rowIndex = dataGridView1.CurrentCell.RowIndex + index; 
     if (rowIndex > -1) 
     { 
      bool pingable = false; 
      Ping pinger = new Ping(); 

      var row = dataGridView1.Rows[rowIndex]; 
      if (row != null) 
      { 
       PingReply reply = pinger.Send(row.Cells[0].Value.ToString()); 

       txtIP.Text = row.Cells[0].Value.ToString(); 
       txtUser.Text = row.Cells[1].Value.ToString(); 
       txtComputer.Text = row.Cells[2].Value.ToString(); 
       txtUnity.Text = row.Cells[3].Value.ToString(); 
       txtSession.Text = row.Cells[4].Value.ToString(); 
       if (pingable = reply.Status == IPStatus.Success) 
       { 
        pictureBoxGreen.Show(); 
        pictureBoxRed.Hide(); 
       } 
       else if (pingable = reply.Status == IPStatus.TimedOut) 
       { 
        pcGreen.Hide(); 
        pcRed.Show(); 
       } 
      } 
     } 
    } 
} 

キーダウンイベントが現在の行を与えるだろう、我々は我々の要件ごとに、このメソッドをオーバーライドする必要があり、私はこれはそれが完全に働いた

+0

、これを試してみてください動作しますが、キーヒットごとに行数を更新しました。ありがとうございました! – Rekcs

+0

私は助けてうれしいです。 –

関連する問題