【问题标题】:Using arrows at a DataGridView in C#在 C# 中的 DataGridView 上使用箭头
【发布时间】:2017-06-20 08:57:35
【问题描述】:

所以,我有一个 DataGridView,我正在从我的 Access 数据库中填充信息。 DataGridView 中的第一列作为我网络中的 IP。我想要做的是使用键盘上的上下箭头,并将每一行的信息显示到几个 TextBox 中。这是代码:

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

【问题讨论】:

    标签: c# 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();
                    }
                }
            }
        }
    }
    

    按键事件将给出当前行,我们需要根据我们的要求覆盖它,我已经根据按键更新了行数,这会起作用,请试试这个,

    【讨论】:

    • 很高兴能够提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多