C#InvalidCastException仅适用于一个值?

时间:2014-08-18 21:25:59

标签: c# datagridview

我有一个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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您的意思是使用ComboBox.SelectedValue吗?这涉及数据绑定,我不能告诉你绑定的内容。

如果您只是将int添加到ComboBox.Items集合中,请使用SelectedItem。此属性也可以返回null ...请注意,null输入Convert.IsDBNull会返回false。请参阅here

最后,将Convert投射到Int32,如此:

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;
        }
    }
}

或者就此而言,您可以使用ComboBox.SelectedIndex代替。