Click here to Skip to main content
11,719,973 members (87,297 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET 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
Posted 6-Apr-12 23:35pm
Edited 19-Apr-12 23:04pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
            }
        }
Regards
Praveen
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

try this:
<itemtemplate>
 <asp:checkbox id="chkEmailReq" runat="server" checked="<%# bool.Parse(Eval("Email_Requriment").ToString()) %>" enabled="false" />
</itemtemplate>

Regards,
Akaas Developer
questions-interviews.com
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You are trying to cast a NULL to a boolean.
Your code :
DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
this means dr["Email_Requriment"] is DBNull.Value so set
dr["Email_Requriment"] = false;
before you do:
dt.Rows.Add(dr);

and like Praveen says:
dt.Columns.Add("Email_Requriment", typeof(bool));
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You can use TemplatField and try this:

<input type="checkbox" id="Checkbox1" <%# Convert.ToBoolean(Eval("Email_Requriment"))==true?"checked" : "" %> />
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 1,869
1 OriginalGriff 770
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 20 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100