我有一个带有组合框的数据网格,我想获得我的价值,我可以得到它,但我不知道为什么,我得到了 4 次???有人可以帮助我吗?
这是我的代码:
private void dgvLocataire_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dgvLocataire.BeginEdit(false);
var ec = dgvLocataire.EditingControl as DataGridViewComboBoxEditingControl;
if (ec != null && ec.Width - e.X < SystemInformation.VerticalScrollBarWidth)
ec.DroppedDown = true;
if ((e.ColumnIndex != 3) && (e.ColumnIndex != 4))
{
dgvLocataire.Columns[e.ColumnIndex].ReadOnly = true;
}
dgvLocataire.CellValueChanged +=
new DataGridViewCellEventHandler(dgvLocataire_CellValueChanged);
//dgvLocataire.CurrentCellDirtyStateChanged +=
//new EventHandler(dgvLocataire_CurrentCellDirtyStateChanged);
}
private void dgvLocataire_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dgvLocataire.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvLocataire_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
string comboboxSelectedValue = string.Empty;
if (dgvLocataire.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewComboBoxColumn))
comboboxSelectedValue = dgvLocataire.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
MessageBox.Show(comboboxSelectedValue);
}
当我在组合框中选择一个值时,消息框出现 4 次。谢谢你的帮助