2017-06-20 1 views
1

So habe ich ein DataGridView, wo ich Informationen aus meiner Access-Datenbank bevölkert. Die erste Spalte von DataGridView als IP von meinem Netzwerk. Was ich versuche, ist, die Pfeile von der Tastatur nach oben und unten zu verwenden und die Informationen aus jeder Zeile in ein paar TextBoxen anzuzeigen. Dies ist der Code:Verwenden von Pfeilen an einem DataGridView in C#

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(); 
      } 
     } 
    } 
} 

Das Problem ist, nachdem in der ersten Reihe des Datagridview zum Beispiel klicken und die Pfeile verwenden, es wird nicht die richtigen Informationen angezeigt werden und stattdessen die Informationen aus der obigen Zeile angezeigt werden soll. Weißt du, was könnte das Problem sein?

Antwort

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(); 
       } 
      } 
     } 
    } 
} 

Key-Down-Ereignis aktuellen Zeile geben, müssen wir dies als pro unsere Anforderung außer Kraft zu setzen, i die Zeilenanzahl per Schlüssel Treffer aktualisiert haben, wird diese bitte arbeiten diese versuchen,

+0

Es funktionierte perfekt. Vielen Dank! – Rekcs

+0

Ich bin froh, Ihnen helfen zu können. –

Verwandte Themen