首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >` `DataGridViewComboBoxCell`‘返回“null”

` `DataGridViewComboBoxCell`‘返回“null”
EN

Stack Overflow用户
提问于 2014-12-24 12:20:13
回答 1查看 670关注 0票数 0

我的DataGridView的第一个单元是ComboBox。我在这个专栏中加入如下.

代码语言:javascript
复制
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事件

代码语言:javascript
复制
if ((this.dataGrid.CurrentRow.Cells[0] as DataGridViewComboBoxCell).Value == null)
{
    MessageBox.Show("You must select one option.");
}

但是该值每次都返回null,即使该值是从ComboBoxCell中选择的。这里,Cell不是null,而是单元格的值为null。

这是怎么回事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-13 11:37:11

我的成就如下..。

代码语言:javascript
复制
    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 { }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27636872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档