在KeyPress上向上和向下移动所选行

时间:2016-07-15 04:41:17

标签: c# .net datagridview

我使用以下代码将数据添加到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。这不起作用。我做错了什么。

1 个答案:

答案 0 :(得分: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;
        }
    }