无法将类型为“System.Windows.Forms.DataGridViewTextBoxCell”的对象强制转换为“System.Windows.Forms.DataGridViewCheckBoxCell”。 [英] Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewCheckBoxCell'.

查看:575
本文介绍了无法将类型为“System.Windows.Forms.DataGridViewTextBoxCell”的对象强制转换为“System.Windows.Forms.DataGridViewCheckBoxCell”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:InValid强制转换异常被捕获

无法强制转换类型为''System.Windows.Forms.DataGridViewTextBoxCell''的对象来键入''System.Windows.Forms.DataGridViewCheckBoxCell''。<下面是
是我的代码,Ple可以帮助我。



ERROR : InValid cast Exception Was Caught
Unable to cast object of type ''System.Windows.Forms.DataGridViewTextBoxCell'' to type ''System.Windows.Forms.DataGridViewCheckBoxCell''.
below is my code , Ple can anyone help me.

private void DgvAllowance_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
            try
            {
                DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)DgvAllowance.CurrentCell;
                DataGridViewTextBoxCell txtpercentage = (DataGridViewTextBoxCell)DgvAllowance["Percentage", e.RowIndex];
                DataGridViewTextBoxCell txtamount = (DataGridViewTextBoxCell)DgvAllowance["Amount", e.RowIndex];
                bool isChecked = (bool)checkbox.EditedFormattedValue;
                if (isChecked)
                {
                    txtpercentage.ReadOnly = false;
                    txtamount.ReadOnly = true;
                }
                else
                {
                    txtamount.ReadOnly = false;
                    txtpercentage.ReadOnly = true;
                }
                txtpercentage.Value = string.Empty;
                txtamount.Value = string.Empty;
            }
            catch(Exception ex) 
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }





Jibesh添加的代码块[/ Edit]

推荐答案

很明显,CurrentCell不是DataGridViewCheckBoxCell - 它是DataGridViewTextBoxCell。



这意味着用户点击了您没想到的地方。所以检查!

用试演替换你的演员:

Fairly clearly, the CurrentCell is not a DataGridViewCheckBoxCell - it''s a DataGridViewTextBoxCell.

Which means that the user has clicked where you didn''t expect. So check!
Replace your cast with a trial cast:
DataGridViewCheckBoxCell checkbox = DgvAllowance.CurrentCell as DataGridViewCheckBoxCell;
if (checkbox != null)
   {
   ...

如果您希望用户点击任何地方,也不要使用CurrentCell ...

Or don''t use CurrentCell if you want the user to click anywhere...


这篇关于无法将类型为“System.Windows.Forms.DataGridViewTextBoxCell”的对象强制转换为“System.Windows.Forms.DataGridViewCheckBoxCell”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆