Click here to Skip to main content
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 7-Apr-12 0:35am
Edited 20-Apr-12 0:04am
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 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 20 Apr 2012
Copyright © CodeProject, 1999-2014
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