We want to attach a combo box into a cell of data grid view. Now it works with mouse click. but for faster moving of form we need it to work using keyboard.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace GridComboSampleCode { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void CreateGrid() { DataTable dt = new DataTable(); dt.Columns.Add("EmpName", typeof(string));//0 dt.Columns.Add("EmpID", typeof(int));//1 dt.Columns.Add("PhoneNo", typeof(string));//2 dt.Columns.Add("Address", typeof(string));//3 dt.Columns.Add("Email", typeof(string));//4 dataGridView1.DataSource = dt; dataGridView1.Controls.Add(comboBox1); } private void Form1_Load(object sender, EventArgs e) { CreateGrid(); fillRecords(1); } string SQL; void fillRecords(int QueryNo) { SqlConnection con = new SqlConnection(@"Data Source=APPLE-PC\RNS;Initial Catalog=DemoDatabase;User ID=sa;Password=rns11"); con.Open(); if (QueryNo==1) { SQL = "Select EmpName,EmpID from EmployeeMaster"; } else if (QueryNo==2) { SQL = "Select PhoneNo,Address from EmployeeMaster where EmpID=" + comboBox1.SelectedValue.ToString(); } SqlDataAdapter da = new SqlDataAdapter(SQL, con); DataTable dt = new DataTable(); da.Fill(dt); con.Close(); if (QueryNo == 1) { comboBox1.DataSource = dt; comboBox1.DisplayMember = "EmpName"; comboBox1.ValueMember = "EmpID"; } else if (QueryNo == 2) { dataGridView1.CurrentRow.Cells[1].Value = comboBox1.SelectedValue.ToString(); dataGridView1.CurrentRow.Cells[0].Value = comboBox1.Text; DataRow dr=dt.Rows[0]; dataGridView1.CurrentRow.Cells[2].Value = dr["PhoneNo"]; dataGridView1.CurrentRow.Cells[3].Value = dr["Address"]; } } private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex==0) { comboBox1.Visible = true; comboBox1.Location = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location; comboBox1.Size = dataGridView1.CurrentCell.Size; } } private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex == 0) { comboBox1.Visible = false; } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedValue.ToString()!="System.Data.DataRowView") { fillRecords(2); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)