简体   繁体   English

输入键的Datagridview事件

[英]Datagridview event for enter key

I have a bound datagridview.when i select a cell in datagridview and press enter, i want a event to be raised which passes the selected cell's row's first column value to another form. 我有一个绑定的datagridview。当我在datagridview中选择一个单元格并按Enter键时,我希望引发一个事件,该事件将所选单元格的行的第一列值传递给另一种形式。 how to do it? 怎么做?

I solved this problem by using _EditingControlShowing Event to the datagridview and using PreviewKeyDown to the cell here the code: 我通过使用_EditingControlShowing Event到datagridview并使用PreviewKeyDown到此单元格的代码解决了这个问题:

DataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler (DataGridView1_EditingControlShowing);

private void dbg_EditingControlShowing (object sender, DataGridViewEditingControlShowingEventArgs e) {
        TextBox txb = e.Control as TextBox;
        txb.PreviewKeyDown += (S, E) => {
            if (E.KeyCode == Keys.Enter) {
                DataGridView1.CurrentCell = dbg.CurrentRow.Cells["Column_name"];
                //Or any code you ...
            }
        };
}
void TextBox1_KeyDown(object sender, KeyEventArgs e)
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
        e.Handled = true; 
    } 
}

void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
{ 
    if (e.KeyCode == Keys.Return)
    {
        /* Your code here! */ 
    }
}

Form 1 Code: 表格1的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        dt.Rows.Add();
        dt.Rows[dt.Rows.Count - 1][0] = "1";
        dt.Rows[dt.Rows.Count - 1][1] = "Stackoverflow";
        dataGridView1.DataSource = dt;
    }
    string ID = string.Empty;
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        ID = dataGridView1.SelectedRows[0].Cells["ID"].Value.ToString();
    }
       private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Form2 frmTwo = new Form2(ID);
            frmTwo.Show();
        }
    }
}

Form 2 Code: 表格2的代码:

public partial class Form2 : Form
{
    public string Id = string.Empty;
    public Form2(string FormOneID)
    {
        InitializeComponent();
        Id = FormOneID;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show("FormOne ID" + Environment.NewLine + Id + Environment.NewLine + "Displaying on FormTwo");
    }
}

Assuming this is for Winforms, you can subscribe to one of the Key events and use that event. 假设这是针对Winforms的,则可以订阅其中一个Key事件并使用该事件。

For example, here is using the KeyUp event. 例如,这里使用KeyUp事件。

 private void myDataGridView_KeyUp(object sender, KeyEventArgs e)
 {
      // nothing is selected
      if (myDataGridView.SelectedRows.Count == 0)
          return;

      if (e.KeyCode == Keys.Enter)
      {
           string firstColumnValue = myDataGridView.SelectedRows[0].Cells[0].Value.ToString();

           // passes the value through the constructor to the 
           //   second form.
           MySecondForm f2 = new MySecondForm(firstColumnValue);
           f2.Show();
      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM