Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# enter键的Datagridview事件_C#_Winforms - Fatal编程技术网

C# enter键的Datagridview事件

C# enter键的Datagridview事件,c#,winforms,C#,Winforms,我有一个绑定的datagridview。当我在datagridview中选择一个单元格并按enter键时,我希望引发一个事件,将所选单元格的行的第一列值传递给另一个窗体。如何操作?假设这是针对Winforms的,您可以订阅其中一个关键事件并使用该事件 例如,这里使用的是KeyUp事件 private void myDataGridView_KeyUp(object sender, KeyEventArgs e) { // nothing is selected if

我有一个绑定的datagridview。当我在datagridview中选择一个单元格并按enter键时,我希望引发一个事件,将所选单元格的行的第一列值传递给另一个窗体。如何操作?

假设这是针对Winforms的,您可以订阅其中一个关键事件并使用该事件

例如,这里使用的是
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();
      }
 }

假设这是针对Winforms的,您可以订阅其中一个关键事件并使用该事件

例如,这里使用的是
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();
      }
 }

我通过使用
\u editingcontrol将
事件显示到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 ...
            }
        };
}

我通过使用
\u editingcontrol将
事件显示到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 ...
            }
        };
}
表格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();
        }
    }
}
表格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");
    }
}
表格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();
        }
    }
}
表格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");
    }
}

你试了什么?这是winforms吗?如何绑定数据?您可以显示一些代码吗?您可以订阅单元格的键事件,然后在找到Enter键时执行操作。您尝试了什么?这是winforms吗?如何绑定数据?您可以显示一些代码吗?您可以订阅单元格的键事件,然后在找到Enter键时执行操作。它发出错误-System.NullReferenceException was unhandled Message=“对象引用未设置为对象的实例”。它发出错误-System.NullReferenceException was unhandled Message=“对象引用未设置为对象的实例。”