【问题标题】:Move Selected Row Up and Down on KeyPress在 KeyPress 上上下移动选定的行
【发布时间】:2016-07-15 04:41:16
【问题描述】:

我使用以下代码将数据添加到 datagridview。

 if (!string.IsNullOrEmpty(text_item.Text))
                {
                    dataGridView1.Visible = true;
                    try
                    {


                        using (SqlConnection conn = new SqlConnection(constr))
                        {

                            try
                            {
                                conn.Open();
                                SqlDataReader myReader = null;

                                string commandText = "SELECT itemname,rate,stock FROM mytable WHERE itemname LIKE @id";
                                SqlCommand command = new SqlCommand(commandText, conn);
                                string searchParam = string.Format("{0}%", text_item.Text);
                                command.Parameters.AddWithValue("@id", searchParam);
                                using (SqlDataAdapter sda = new SqlDataAdapter(command))
                                {
                                    using (DataTable dt = new DataTable())
                                    {
                                        sda.Fill(dt);
                                        dataGridView1.DataSource = dt;
                                    }
                                }

                            }
                            catch (Exception err)
                            {
                                MessageBox.Show(err.Message);
                            }

                        }
                    }
                    catch (Exception error)
                    {

                    }

                }
                else
                {
                  //  dataGridView1.Visible = false;
                }

当用户点击向上和向下键时,我需要一种方法来选择行。所以我尝试了

dataGridView1.KeyDown+=new KeyEventHandler(dataGridView1_KeyDown);
void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {

        if (e.KeyCode.Equals(Keys.Up))
        {
            moveUp();
        }
        if (e.KeyCode.Equals(Keys.Down))
        {
            moveDown();
        }
        e.Handled = true;
    }


private void moveUp()
        {
            int x = dataGridView1.CurrentCell.RowIndex + 1;
            dataGridView1.Rows[x].Selected = true;
        }

SelectionMode 设置为 FullRowSelect 。这不起作用。我做错了什么。

【问题讨论】:

标签: c# .net datagridview


【解决方案1】:

试过了,效果很好:

    private void MainWindow_Load(object sender, EventArgs e)
    {
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        dataGridView1.MultiSelect = false;
    }

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            try
            {
                int x = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows[x - 1].Selected = true;
            }
            catch
            {
                // Index Out Of Range Ex
            }
        }

        if (e.KeyCode == Keys.Down)
        {
            try
            {
                int x = dataGridView1.CurrentCell.RowIndex;
                dataGridView1.Rows[x + 1].Selected = true;
            }
            catch
            {
                // Index Out Of Range Ex
            }
        }

        if (e.KeyCode == Keys.Enter)
        {
            e.SuppressKeyPress = true;
        }
    }

【讨论】:

  • 但是即使按下“输入”键,行选择也会向下移动
  • 添加这个:if (e.KeyCode == Keys.Enter) { e.SuppressKeyPress = true; }
  • 我在文本框的 textchange 事件上填充 datagridview...我如何专注于 datagridview ..以便处理按钮按下事件..
  • 我更新了答案中的代码,这应该执行“移动”并禁止“输入”工作 :) 使用它来关注:dataGridView1.Focus(); 并选择事件中的第一行: dataGridView1.Rows[0].Selected = true;
猜你喜欢
  • 2010-11-04
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 2012-02-28
相关资源
最近更新 更多