DataGridView“Enter”关键事件处理 [英] DataGridView "Enter" key event handling

查看:196
本文介绍了DataGridView“Enter”关键事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView填充有DataTable,有10列。当我点击Enter键时,我有一个方案从一行移动到另一行,那么我需要选择该行,并且需要具有该行值。



但是在这里当我选择 n 行时,它会自动移动到 n + 1 行。



请帮助我...



在页面加载事件中

  SqlConnection con = 
new SqlConnection(Data Source =; Initial Catalog = MHS; User ID = mhs_mt;密码= @ mhsinc);

DataSet ds = new System.Data.DataSet();
SqlDataAdapter da = new SqlDataAdapter(select * from MT_INVENTORY_COUNT,con);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables [0];

然后,

 code> private void dataGridView1_KeyPress(object sender,KeyPressEventArgs e)
{
if(e.KeyChar ==(Char)Keys.Enter)
{
int i = dataGridView1.CurrentRow.Index;
MessageBox.Show(i.ToString());
}
}

private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{
int i = dataGridView1.CurrentRow.Index;
MessageBox.Show(i.ToString());
}


解决方案

这是DataGridView,以及第三方供应商在其他数据网格中的标准。



以下是发生的情况:


  1. 用户点击Enter键

  2. DataGridView接收KeyPress事件并执行各种操作(如结束编辑等),然后将单元格向下移动一个

  3. 然后DataGridView检查是否有任何事件处理程序与您联系起来并触发这些事件。

所以在按下回车键的时候,当前的单元格已经改变了。



如果你想要获得行在DataGridView更改该行之前,该用户是在的。这应该符合您现有的代码(显然,您需要为其添加事件处理程序):

  void dataGridView1_PreviewKeyDown(object发件人,PreviewKeyDownEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
int i = dataGridView1.CurrentRow.Index;
MessageBox.Show(i.ToString());
}
}

我希望能帮助您指出正确的方向。不确定你希望在这里做什么,但希望这将解释你看到的是什么。


I have a DataGridView populated with DataTable, have 10 columns. I have a scenario when moving from one row to another when I click on Enter key then I need that row should be selected and need to have that row values.

But here when I select n-th row then it automatically moves to n+1 Row.

Please help me in this...

In Page Load Event:

SqlConnection con = 
    new SqlConnection("Data Source=.;Initial Catalog=MHS;User ID=mhs_mt;Password=@mhsinc");

DataSet ds = new System.Data.DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from MT_INVENTORY_COUNT", con);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];

Then,

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyChar == (Char)Keys.Enter)
     {
           int i = dataGridView1.CurrentRow.Index;
           MessageBox.Show(i.ToString());
     }     
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int i = dataGridView1.CurrentRow.Index;
    MessageBox.Show(i.ToString());
}

解决方案

That's the default behaviour of the DataGridView, and pretty standard in other data grids by 3rd party vendors too.

Here is what happens:

  1. The user hits the enter key
  2. The DataGridView receives the KeyPress event and performs various actions (such as ending edits, etc), and then moves the cell down one row.
  3. Then the DataGridView checks to see if there are any event handlers hooked up by you and fires those.

So by the time the enter key is pressed, the current cell has already changed.

You could use the following if you want to get the row that the user was on before the DataGridView changes the row. This should fit in with your existing code (obviously you will need to add the event handler for it):

void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        int i = dataGridView1.CurrentRow.Index;
        MessageBox.Show(i.ToString());
    }     
}

I hope that helps point you in the right direction. Not sure what you are hoping to do here, but hopefully this explains what you are seeing happen.

这篇关于DataGridView“Enter”关键事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆