Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C仅针对一个值的InvalidCastException?_C#_Datagridview - Fatal编程技术网

C# C仅针对一个值的InvalidCastException?

C# C仅针对一个值的InvalidCastException?,c#,datagridview,C#,Datagridview,我有一个datagridview,我有一些代码来查看一些下拉列表是否已更改 private void tbl_TransactionsDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox combo = e.Control as ComboBox; if (combo != null

我有一个datagridview,我有一些代码来查看一些下拉列表是否已更改

        private void tbl_TransactionsDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox combo = e.Control as ComboBox;
        if (combo != null && tbl_TransactionsDataGridView.CurrentCell.ColumnIndex == 3)
        {
            // Remove an existing event-handler, if present, to avoid  
            // adding multiple handlers when the editing control is reused.
            combo.SelectedIndexChanged -=
                new EventHandler(ComboBox_SelectedIndexChanged);

            // Add the event handler. 
            combo.SelectedIndexChanged +=
                new EventHandler(ComboBox_SelectedIndexChanged);
        }

        if (combo != null && tbl_TransactionsDataGridView.CurrentCell.ColumnIndex == 6)
        {
            // Remove an existing event-handler, if present, to avoid  
            // adding multiple handlers when the editing control is reused.
            combo.SelectedIndexChanged -=
                new EventHandler(Status_SelectedIndexChanged);

            // Add the event handler. 
            combo.SelectedIndexChanged +=
                new EventHandler(Status_SelectedIndexChanged);
        }
    }
当ColumnIndex为6时,它跳转到以下代码。令人恼火的是,这适用于我从下拉列表中选择的除1以外的任何值。如果我选择1,我会得到一个InvalidCastException,告诉我需要使用一个小于无穷大的数字。如果我选择任何其他数字,一切都会按预期的方式启动。我做错了什么

        private void Status_SelectedIndexChanged(object sender, EventArgs e)
    {
        object oStatus = new object();
        oStatus = ((ComboBox)sender).SelectedValue;

        if (!Convert.IsDBNull(oStatus))
        {
            SendKeys.Send("{TAB}");
            if (Convert.ToInt32(oStatus) != 1)
            {
                tbl_TransactionsDataGridView.CurrentRow.Cells["CheckInEmployee"].Value = Environment.UserName;
                tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = DateTime.Now;
            }

            if (Convert.ToInt32(oStatus) == 1)
            {
                tbl_TransactionsDataGridView.CurrentRow.Cells["CheckOutEmployee"].Value = Environment.UserName;
                tbl_TransactionsDataGridView.CurrentRow.Cells["CheckInEmployee"].Value = null;
                tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = null;
            }
        }
    }
您的意思是使用ComboBox.SelectedValue吗?这涉及到数据绑定,我不知道您要绑定到什么

如果只是向ComboBox.Items集合添加int,请使用SelectedItem。此属性也可以返回null。。。请注意,对于空输入,Convert.IsDBNull返回false。看

最后,将其强制转换为Int32,如下所示:


或者,您可以使用ComboBox.SelectedIndex来代替它。

哪行代码引发异常?如果Convert.ToInt32oStatus!=1来自我的原始代码,如果是intoStatus!=1根据下面建议的解决方案。oStatus中对象的类型是什么?异常消息的确切文本是什么?我尝试了上述建议,但得到了相同的错误。下一行正在引发异常如果你是塔塔斯!=1`您能告诉我们您在组合框中填充的代码吗?另外,能否在if intoStatus!=1,看奥斯塔斯的类型?
private void Status_SelectedIndexChanged(object sender, EventArgs e)
{
    object oStatus = new object();
    oStatus = ((ComboBox)sender).SelectedItem;

    if (oStatus != null && !Convert.IsDBNull(oStatus))
    {
        SendKeys.Send("{TAB}");
        if (((int)oStatus) != 1)
        {
            tbl_TransactionsDataGridView.CurrentRow.Cells["CheckInEmployee"].Value = Environment.UserName;
            tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = DateTime.Now;
        }
        else
        {
            tbl_TransactionsDataGridView.CurrentRow.Cells["CheckOutEmployee"].Value = Environment.UserName;
            tbl_TransactionsDataGridView.CurrentRow.Cells["CheckInEmployee"].Value = null;
            tbl_TransactionsDataGridView.CurrentRow.Cells["CloseDate"].Value = null;
        }
    }
}