经过长时间在网上搜索我的问题后,我问了一个问题......
我在2个第一列中有一个DataGridView和DataGridViewComboBoxColumn。 当我手动手动最后一行的第一列的值时,如果需要,我会在最后一行之后添加一个新的空白行。
当我手动更改 第二列的值时,我会更改下一列的类型(文本或组合)并填充DataGridViewComboBoxCell。
这两点与EditingControlShowing事件完美配合,可以激发其他事件。我使用这个事件whitch是唯一一个将ComboBox作为e.Control返回。
我的问题是当我想以编程方式填充col 1&的值时第2列,EditingControlShowing事件不会按原样触发。
我尝试使用BeginEdit属性,使用EditMode = EditProgrammatically属性,但没有更多成功。
这是我的代码的一部分(这不是项目,而是样本的一部分,如果需要我可以发送更多):
private void btnFill_Click(object sender, EventArgs e)
{
List<string[]> sFilters = new List<string[]>();
string[] filter1 = { "col3", "NOT IN" };
string[] filter2 = { "col5", "BETWEEN" };
sFilters.Add(filter1);
sFilters.Add(filter2);
iRow = -1;
foreach (var sVar in sFilters)
{
iRow++;
try
{
iCol = 0;
dataGridView1.CurrentCell = dataGridView1.Rows[iRow].Cells[iCol];
dataGridView1.BeginEdit(true);
dataGridView1.CurrentCell.Value = sVar[0];
//dataGridView1.EndEdit();
}
catch (Exception Ex)
{
MessageBox.Show("btnFill_Click - iCol=0 - iRow=" + iRow.ToString() + " :\n\n" + Ex.Message);
}
try
{
iCol = 1;
dataGridView1.CurrentCell = dataGridView1.Rows[iRow].Cells[iCol];
//dataGridView1.BeginEdit(true);
dataGridView1.CurrentCell.Value = sVar[1];
//dataGridView1.EndEdit();
}
catch (Exception Ex)
{
MessageBox.Show("btnFill_Click - iCol=1 - iRow=" + iRow.ToString() + " :\n\n" + Ex.Message);
}
}
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
iCol = ((DataGridView)(sender)).CurrentCell.ColumnIndex;
iRow = ((DataGridView)(sender)).CurrentCell.RowIndex;
if (iCol == 0)
{
try
{
// Déclenchement d'un évènement quand 'cboVar' change
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// first remove event handler to keep from attaching multiple
combo.SelectedIndexChanged -= new EventHandler(dataGridView1_cboVar_SelectedIndexChanged);
// now attach the event handler
combo.SelectedIndexChanged += new EventHandler(dataGridView1_cboVar_SelectedIndexChanged);
}
((DataGridView)sender).ClearSelection();
}
catch (Exception Ex)
{
MessageBox.Show("EditingControlShowing() - Col0 :\n\n" + Ex.Message);
}
}
else if (iCol == 1)
{
try
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// first remove event handler to keep from attaching multiple
combo.SelectedIndexChanged -= new EventHandler(dataGridView1_cboOpe_SelectedIndexChanged);
// now attach the event handler
combo.SelectedIndexChanged += new EventHandler(dataGridView1_cboOpe_SelectedIndexChanged);
}
}
catch (Exception Ex)
{
MessageBox.Show("EditingControlShowing() - Col1 :\n\n" + Ex.Message);
}
}
}
private void dataGridView1_cboVar_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
comboVar = sender as ComboBox;
if (iCol == 0)
{
if (comboVar.SelectedIndex > 0)
{
if (iRow == dataGridView1.RowCount - 1)
dataGridView1.Rows.Add();
}
comboVar.SelectedIndexChanged -= new EventHandler(dataGridView1_cboVar_SelectedIndexChanged);
}
}
catch (Exception Ex)
{
MessageBox.Show("dataGridView1_cboVar_SelectedIndexChanged() :\n\n" + Ex.Message);
}
}
我希望我能清楚地知道......
非常感谢你的帮助,对不起我的英语不好......
的Herve