C# 在C中使用DataGridView上的箭头#

C# 在C中使用DataGridView上的箭头#,c#,datagridview,C#,Datagridview,因此,我有一个DataGridView,在这里我从Access数据库填充信息。DataGridView中的第一列是来自我的网络的IP。我试图做的是使用键盘上的上下箭头,将每一行的信息显示在两个文本框中。代码如下: private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { bool pingable = false; Ping pinger = new Ping(); foreach (DataG

因此,我有一个DataGridView,在这里我从Access数据库填充信息。DataGridView中的第一列是来自我的网络的IP。我试图做的是使用键盘上的上下箭头,将每一行的信息显示在两个文本框中。代码如下:

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的第一行单击并使用箭头后,它不会显示正确的信息,而是显示上面一行的信息。你知道有什么问题吗

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

按键按下事件将给出当前行,我们需要根据我们的要求覆盖该行,我已根据按键点击更新了行数,这将起作用,请尝试此操作,

我很高兴能够提供帮助。