Forcing Default button on a gridview

血红的双手。 提交于 2019-11-30 18:41:28

问题


I'm using gridview with templates to show and edit some information from a sql database.

When I edit and change the data in that row and then click enter it automatically presses the highest on page button which uses submit to server set to true which means it'll try to delete instead of update.

I've have tried setting a panel round the gridview and setting the panel's default button to the "updatebutton" but it won't allow that because it can't 'see' the buttons.


回答1:


I had a similar problem and I found a very simple workaround:

  1. Place a button below the GridView and make it invisible by CSS (i.e. position: relative; left: -2000px)
  2. Create a Panel around the GridView and give it as DefaultButton the ID of the button we just created.
  3. Write the following line of code for the click-event of the button: myGridView.UpdateRow(myGridView.EditIndex, false);

Whenever you press enter in the GridView now, the edited row will be confirmed.




回答2:


You need to precess KeyDown or KeyPress event of the grid, and check if pressed key if Keys.Enter :

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

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            button1_Click(this, EventArgs.Empty);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Your logic here
    }
}


来源:https://stackoverflow.com/questions/1389234/forcing-default-button-on-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!