开发者

Relation Of KeyDown Event With Datagridview's RowHeaderVisible

开发者 https://www.devze.com 2023-01-18 20:47 出处:网络
Software I use: C#, VS-2005. I have a populated combobox in datagridview. I have noticed that when the datagridview property RowHeaderVisible is set to false then no keydown event is fired on editing

Software I use: C#, VS-2005.

I have a populated combobox in datagridview. I have noticed that when the datagridview property RowHeaderVisible is set to false then no keydown event is fired on editing control. But when I set RowHeaderVisible to true, the keydown event works fine.

So, I know that the RowHeaderVisible property set to true will work. But there is no need to set it to true as per my requirement.

I want to know the reason for this behavior, and how to overcome it.

My Code Is :

private void Form1_Load(object sender, EventArgs e)

{          
        string consstr = "server=.;initial   
        catalog=mydatabase;uid=myuserid;pwd=mypassword";
        SqlConnection conn = new SqlConnection(consstr);
        conn.Open();
        string sql = "select accname from AccountMast";
        SqlDataAdapter dap = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        DataTable dt = new DataTable();
        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        dataGridView1.Columns.Add(cmb);
        cmb.DataSource = ds.Tables[0];
        cmb.DisplayMember = "accname";
        cmb.ValueMember = "accname";
        dt.Columns.Add("AMOUNT", typeof(decimal));
        dataGridView1.DataSource = dt;
        dataGridView1.Columns[0].Width = 300;
        dataGridView1.Columns[1].Width = 143;
        dataGridView1.Columns[1].DefaultCellStyle.Format = "f2";
    }
   private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Alt == true && e.KeyCode == Keys.C) 
        {
            Form2 f2 = new Form2();
            f2.Show();
         }
    }

The above code is represent entry point. I mean user e开发者_运维技巧nter the data on it. I have populate combobox in datagridview with database of sql. The combobox having list of account name. user select it as per their demand and than enter data on column of amount and save it.

That's all. but there is a problem you can see it. as per my comment lying below the "tergive's" answer.


Here is an alternate form using ProcessCmdKey on the form instead of using the custom DGV:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    DataGridView dgv;

    public Form1()
    {
        Text = "Form1";

        dgv = new DataGridView();
        dgv.Dock = DockStyle.Fill;
        DataGridViewComboBoxColumn dgvColumn = new DataGridViewComboBoxColumn();
        dgvColumn.HeaderText = "Header";
        dgvColumn.DataSource = new string[] { "One", "Two", "Three", "Four" };
        dgv.Columns.Add(dgvColumn);
        Controls.Add(dgv);

        Button button = new Button();
        button.Text = "Place holder";
        button.Dock = DockStyle.Top;
        Controls.Add(button);
    }

    [DllImport("user32.dll")]
    private static extern bool IsChild(IntPtr hwndParent, IntPtr hwndChild);

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if ((keyData & (Keys.Alt | Keys.C)) == (Keys.Alt | Keys.C))
        {
            if (dgv.Handle == msg.HWnd || IsChild(dgv.Handle, msg.HWnd))
            {
                Form form = new Form();
                form.Text = "Form2";
                form.Show(this);
                return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}


Control doesn't provide a good way to externally wire in a "command key" like that. The best place to look for command keys is to override ProcessCmdKey. So you basically have two choices:

1) Override ProcessCmdKey on your form and check during invocation if the DGV (or one of its children) has keyboard focus.

2) Create a custom DGV and override ProcessCmdKey there.

I have used #2 in the past and created something as follows to allow external access to the event (the parent form).

Expanded example

using System;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    public Form1()
    {
        Text = "Form1";

        CustomDataGridView dgv = new CustomDataGridView();
        dgv.Dock = DockStyle.Fill;
        DataGridViewComboBoxColumn dgvColumn = new DataGridViewComboBoxColumn();
        dgvColumn.HeaderText = "Header";
        dgvColumn.DataSource = new string[] { "One", "Two", "Three", "Four" };
        dgv.Columns.Add(dgvColumn);
        dgv.CommandKeyPress += new CommandKeyPressHandler(dgv_CommandKeyPress);
        Controls.Add(dgv);

        Button button = new Button();
        button.Text = "Place holder";
        button.Dock = DockStyle.Top;
        Controls.Add(button);
    }

    bool dgv_CommandKeyPress(object sender, Keys keyData)
    {
        if ((keyData & (Keys.Alt | Keys.C)) == (Keys.Alt | Keys.C))
        {
            Form form = new Form();
            form.Text = "Form2";
            form.Show(this); 
            return true;
        }
        return false;
    }
}

delegate bool CommandKeyPressHandler(object sender, Keys keyData);

class CustomDataGridView : DataGridView
{
    public event CommandKeyPressHandler CommandKeyPress;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        CommandKeyPressHandler eventDelegate = CommandKeyPress;
        if (eventDelegate != null)
        {
            foreach (CommandKeyPressHandler handler in eventDelegate.GetInvocationList())
            {
                if (handler(this, keyData))
                    return true;
            }
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消