将System.Windows.Form.ComboBox放在Datagridview中

本文关键字:放在 Datagridview ComboBox Form System Windows | 更新日期: 2023-09-27 18:26:49

我们想在数据网格视图的单元格中附加一个组合框。现在它可以通过鼠标点击来工作。但为了更快地移动形式,我们需要它使用键盘。

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); // Add System.Windows.Form.ComboBox in Datagridview
    }
    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)
    {
        //Visible Location on Current Cell
        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);
        }
    }
}

将System.Windows.Form.ComboBox放在Datagridview中

此解由Sinisa Hajnal给出。它应该使用键盘工作,但通常需要先按F2键才能进入编辑模式。我可以通过处理CellEnter事件并打开组合框的编辑模式并在内部设置焦点来解决这个问题。

`

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;
               comboBox1_SelectedIndexChanged(null, null);
                comboBox1.Focus();//focus on Combobox
            }
        }

`