2015-11-28 96 views
0

我有一個包含多列的DataGridView,第一列類型是Combobox。當CurrentCell發生變化時,DataGridView ComboBox會丟失它的值

我試圖把重點放在下一個(第2)小區時,組合框的選擇被改變(通過點擊鼠標或按一個鍵)

我居然做到了,但問題是話題說什麼:如果我干擾.CurrentCell以便將焦點集中到下一個單元格,單元格內的組合框項會丟失其值。

如果我按<輸入,只保留其值>或<標籤>更改組合框的值

DataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridView1_EditingControlShowing); 

private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    e.CellStyle.BackColor = Color.Red; 
    e.CellStyle.ForeColor = Color.White; 

    if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl)) 
    { 
     Console.WriteLine("adding handler"); 
     ComboBox cmb = (ComboBox)e.Control; 
     //cmb.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged); 
     //cmb.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged); 

     cmb.SelectionChangeCommitted -= new EventHandler(cmb_SelectionChangeCommitted); 
     cmb.SelectionChangeCommitted += new EventHandler(cmb_SelectionChangeCommitted); 
    } 

} 
void cmb_SelectionChangeCommitted(object sender, EventArgs e) 
{ 
    ComboBox cmb = (ComboBox)sender; 
Console.WriteLine("Index #{0} Value:{1}", cmb.SelectedIndex.ToString(), cmb.SelectedValue.ToString()); //its working 

    DataGridViewCell c = DataGridView1.CurrentCell; //get current cell 
    DataGridView1.CurrentCell = DataGridView1[1, c.RowIndex]; //skips to next cell on the same row 

    //here combobox lost its value after focusing on another cell! 
} 

回答

0

確定後,關鍵的,我想通了。我用SenKeys.Send來解決。

相關問題