CurrentCell更改时,DataGridView组合框将丢失其值

本文关键字:组合 DataGridView CurrentCell | 更新日期: 2023-09-27 18:24:53

我有一个包含多列的DataGridView,第一列类型是Combobox。

当组合框选择发生变化时(通过鼠标点击或按键),我正试图将注意力集中在下一个(第二个)单元格上

我确实做到了,但问题正是主题所说的:如果我为了关注下一个单元格而干扰.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!
}

CurrentCell更改时,DataGridView组合框将丢失其值

好吧,我想通了。我用了SenKeys。发送出去四处走动。