System.InvalidCastException:指定的强制转换在gridview中无效 [英] System.InvalidCastException: Specified cast is not valid In gridview

查看:127
本文介绍了System.InvalidCastException:指定的强制转换在gridview中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当gridview为空时,我只能面对页脚部分.


I have facing problem when gridview is empty then show only footer part.


private void FillCustomerInGrid()
    {
        gvGroupList.DataSource = AdminService.SelectGroupUsergrid();
        gvGroupList.DataBind();
        int rowCount = gvGroupList.Rows.Count;
        if (rowCount == 0)
        {
            DataTable dt = new DataTable();
           dt.Columns.Add("Id");
            dt.Columns.Add("Group_Name");
            dt.Columns.Add("emp_name");
            dt.Columns.Add("emp_Id");
            dt.Columns.Add("Email_Requriment");
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            gvGroupList.DataSource = dt;
            gvGroupList.DataBind();
            gvGroupList.Rows[0].Visible = false;

        }
    }



但是问题在
发生



But the problem is happening when in

<itemtemplate>
    <asp:CheckBox ID="chkEmailReq" runat="server" Checked='<%#Eval("Email_Requriment") %>'

    Enabled="false" />
</itemtemplate>


然后给出错误-


Then it gives error --

System.InvalidCastException: Specified cast is not valid 


请帮助我


please help me

推荐答案

将数据类型添加到列中,默认情况下为字符串,为


Add data types to the columns, by default it is string as


private void FillCustomerInGrid()
        {
            gvGroupList.DataSource = AdminService.SelectGroupUsergrid();
            gvGroupList.DataBind();
            int rowCount = gvGroupList.Rows.Count;
            if (rowCount == 0)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Id");
                dt.Columns.Add("Group_Name");
                dt.Columns.Add("emp_name");
                dt.Columns.Add("emp_Id");
                dt.Columns.Add("Email_Requriment", typeof(bool));
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                gvGroupList.DataSource = dt;
                gvGroupList.DataBind();
                gvGroupList.Rows[0].Visible = false;
            }
        }


问候
Praveen


Regards
Praveen




试试这个:

Hi,

Try this:

private void FillCustomerInGrid()
    {
        gvGroupList.DataSource = AdminService.SelectGroupUsergrid();
        gvGroupList.DataBind();
        int rowCount = gvGroupList.Rows.Count;
        if (rowCount == 0)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("Group_Name");
            dt.Columns.Add("emp_name");
            dt.Columns.Add("emp_Id");
            dt.Columns.Add("Email_Requriment");
            dt.Rows.Add("", "", "", false);
            gvGroupList.DataSource = dt;
            gvGroupList.DataBind();
            gvGroupList.Rows[0].Visible = false;
 
        }
    }



我希望这对您有用.



I hope this will work for you..


您正在尝试将NULL转换为布尔值.
您的代码:
You are trying to cast a NULL to a boolean.
Your code :
DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);


这意味着dr ["Email_Requriment"]是DBNull.Value,因此设置


this means dr["Email_Requriment"] is DBNull.Value so set

dr["Email_Requriment"] = false;

,然后再执行以下操作:

before you do:

dt.Rows.Add(dr);



就像Praveen所说:



and like Praveen says:

dt.Columns.Add("Email_Requriment", typeof(bool));


这篇关于System.InvalidCastException:指定的强制转换在gridview中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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