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 enter 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.