我的DataGridView的第一个单元是ComboBox。我在这个专栏中加入如下.
DataTable dt = new DataTable();
string qry = "SELECT [NAME] FROM [PERSONS]";
// running ExcecuteNonQuery() function in globalData.cs file
dt = globalData.q.select(qry, globalData.connectionstring);
foreach (DataRow row in dt.Rows)
{
(this.dataGrid.Columns["Name"] as DataGridViewComboBoxColumn).Items.Add(row[0].ToString());
}和检查Cell_Leave事件
if ((this.dataGrid.CurrentRow.Cells[0] as DataGridViewComboBoxCell).Value == null)
{
MessageBox.Show("You must select one option.");
}但是该值每次都返回null,即使该值是从ComboBoxCell中选择的。这里,Cell不是null,而是单元格的值为null。
这是怎么回事?
发布于 2015-01-13 11:37:11
我的成就如下..。
private void DataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dataGrid.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.KeyDown += this.DataGridComboBox_KeyDown;
}
}
private void DataGridComboBox_KeyDown(object sender, KeyEventArgs e)
{
if (!(e.Control && e.KeyCode == Keys.S) && !(e.Control && e.KeyCode == Keys.C))
{
try
{
var currentcell = this.dataGrid.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
DataGridViewComboBoxCell cel = (DataGridViewComboBoxCell)this.dataGrid.Rows[currentcell.Y].Cells[0];
cel.Value = sendingCB.EditingControlFormattedValue.ToString();
}
catch { }
}
}https://stackoverflow.com/questions/27636872
复制相似问题