Click here to Skip to main content
15,905,233 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


C#
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);
            }
        }
       


    }
}
Posted

1 solution

It should work using keyboard, but usually you need to press F2 first to enter edit mode. You can solve this by handling CellEnter event and opening edit mode for combobox and setting focus to it inside.

If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
ranjeet11 17-Oct-14 1:57am    
can you write code please
Sinisa Hajnal 17-Oct-14 2:18am    
I can. I did before that's how I know it is possible :) Will I? No. I'm at my own work, I described exactly what needs to be done. This boards are not free code service. Do your own work. We're offering our time for advice, not as free work force. Thank you for understanding.
It can easily be found on google, search code project, stack overflow or dream in code and you'll find the examples.
ranjeet11 17-Oct-14 3:32am    
Thank you so much................
ranjeet11 17-Oct-14 3:33am    
Solution-

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex==0)
{
SendKeys.Send("{F2}");
comboBox1.Visible = true;
comboBox1.Location = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
comboBox1.Size = dataGridView1.CurrentCell.Size;
comboBox1.Focus();
}
}
Sinisa Hajnal 17-Oct-14 4:09am    
Thats a good start. Although you'd better use specialized functions for entering edit mode rather then sending keys:
dataGridView1.CurrentCell = cell; <- this is probably useless since you're entering the cell :)
dataGridView1.BeginEdit(true);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900