2014-10-16 47 views
1

我們希望將組合框附加到數據網格視圖的單元格中。現在它可以用鼠標點擊。但爲了更快地移動表單,我們需要使用鍵盤來工作。將System.Windows.Form.ComboBox放在Datagridview中

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

回答

0

該解決方案由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 
      } 
     } 

`

相關問題