简体   繁体   English

如何在网格视图中绑定复选框

[英]how to to bind check box in grid view

i have grid view and i binds up this check box like this : 我有网格视图,我将这个复选框像这样绑定起来:

<ItemTemplate>
<asp:CheckBox ID="chk_UserOFC1" runat="server" ToolTip="User of Easy Office" Checked='<%#Eval("UserOFC")%>'/>
</ItemTemplate>

how ever this field was nullable bit so run time error occurs : 此字段是如何为空的位,因此发生运行时错误:

Specified cast is not valid. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error: 


Line 1230:                    </HeaderTemplate>
Line 1231:                    <ItemTemplate>
Line 1232:                    <asp:CheckBox ID="chk_UserOFC1" runat="server" ToolTip="User of Easy Office" Checked='<%#Bind("UserOFC")%>'/>
Line 1233:                    </ItemTemplate>
Line 1234:                    <HeaderStyle CssClass="headinglist_bg" HorizontalAlign="Left" />


Source File: f:\CRM\Staff\Raise_Ticket.aspx    Line: 1232 

---------------------------Updated------------------------------------ - - - - - - - - - - - - - -更新 - - - - - - - - - - - --------------

 public void FillGrid(string GroupByText, string ColumnName, string SearchText)
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var query = (dynamic)null;
            switch (GroupByText)
            {
                case "Customer":
                    query = db.Parties.Where(p => p.type_id.Equals("1")).OrderByDescending(p => p.Register_Date).Select(p => new
                    {
                        Id = p.Id,
                        Name = p.Name,
                        type_id = p.type_id,
                        Ownername = p.Ownername,
                        Ownermob = p.Ownermob,
                        ContactPerson = p.ContactPerson,
                        ContactPersonmob = p.ContactPersonmob,
                        Phone = p.Phone,
                        Mobile = p.Mobile,
                        Room = p.Room,
                        Build = p.Build,
                        Road = p.Road,
                        Area = p.Area,
                        City = p.City,
                        country_id = p.country_id,
                        state_id = p.state_id,
                        Email = p.Email,
                        Remark = p.Remark,
                        UserOFC = p.UserOFC,
                        UserVAT = p.UserVAT,
                        UserINV = p.UserINV,
                        UserNone = p.UserNone,
                        Username = db.Users.Where(u => u.Ref_no.Equals(p.User_id)).Select(u => u.Ref_no).FirstOrDefault(),
                        Register_Date = p.Register_Date
                    }).FilterForColumn(ColumnName, SearchText).ToList();
                    break;
                case "Enquiry":
                    query = db.Parties.Where(p => p.type_id.Equals("2")).OrderByDescending(p => p.Register_Date).Select(p => new
                    {
                        Id = p.Id,
                        Name = p.Name,
                        type_id = p.type_id,
                        Ownername = p.Ownername,
                        Ownermob = p.Ownermob,
                        ContactPerson = p.ContactPerson,
                        ContactPersonmob = p.ContactPersonmob,
                        Phone = p.Phone,
                        Mobile = p.Mobile,
                        Room = p.Room,
                        Build = p.Build,
                        Road = p.Road,
                        Area = p.Area,
                        City = p.City,
                        country_id = p.country_id,
                        state_id = p.state_id,
                        Email = p.Email,
                        Remark = p.Remark,
                        UserOFC = p.UserOFC,
                        UserVAT = p.UserVAT,
                        UserINV = p.UserINV,
                        UserNone = p.UserNone,
                        Username = db.Users.Where(u => u.Ref_no.Equals(p.User_id)).Select(u => u.Ref_no).FirstOrDefault(),
                        Register_Date = p.Register_Date
                    }).FilterForColumn(ColumnName, SearchText).ToList();
                    break;
                default:
                    query = db.Parties.OrderByDescending(p => p.Register_Date).Select(p => new
                    {
                        Id = p.Id,
                        Name = p.Name,
                        type_id = p.type_id,
                        Ownername = p.Ownername,
                        Ownermob = p.Ownermob,
                        ContactPerson = p.ContactPerson,
                        ContactPersonmob = p.ContactPersonmob,
                        Phone = p.Phone,
                        Mobile = p.Mobile,
                        Room = p.Room,
                        Build = p.Build,
                        Road = p.Road,
                        Area = p.Area,
                        City = p.City,
                        country_id = p.country_id,
                        state_id = p.state_id,
                        Email = p.Email,
                        Remark = p.Remark,
                        UserOFC = p.UserOFC,
                        UserVAT = p.UserVAT,
                        UserINV = p.UserINV,
                        UserNone = p.UserNone,
                        Username = db.Users.Where(u => u.Ref_no.Equals(p.User_id)).Select(u => u.Ref_no).FirstOrDefault(),
                        Register_Date = p.Register_Date
                    }).FilterForColumn(ColumnName, SearchText).ToList();
                    break;
            }
            int count = 0;
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id", typeof(int)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("type_id", typeof(int)));
            dt.Columns.Add(new DataColumn("Ownername", typeof(string)));
            dt.Columns.Add(new DataColumn("Ownermob", typeof(string)));
            dt.Columns.Add(new DataColumn("ContactPerson", typeof(string)));
            dt.Columns.Add(new DataColumn("ContactPersonmob", typeof(string)));
            dt.Columns.Add(new DataColumn("Phone", typeof(string)));
            dt.Columns.Add(new DataColumn("Mobile", typeof(string)));
            dt.Columns.Add(new DataColumn("Room", typeof(string)));
            dt.Columns.Add(new DataColumn("Build", typeof(string)));
            dt.Columns.Add(new DataColumn("Road", typeof(string)));
            dt.Columns.Add(new DataColumn("Area", typeof(string)));
            dt.Columns.Add(new DataColumn("City", typeof(string)));
            dt.Columns.Add(new DataColumn("country_id", typeof(int)));
            dt.Columns.Add(new DataColumn("state_id", typeof(int)));
            dt.Columns.Add(new DataColumn("Email", typeof(string)));
            dt.Columns.Add(new DataColumn("Remark", typeof(string)));
            dt.Columns.Add(new DataColumn("UserOFC", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("UserVAT", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("UserINV", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("UserNone", typeof(Boolean)));
            dt.Columns.Add(new DataColumn("Username", typeof(string)));
            dt.Columns.Add(new DataColumn("Register_Date", typeof(DateTime)));
            foreach (var item in query)
            {
                if (item != null)
                {
                    count++;
                    DataRow dr = dt.NewRow();
                    dr["Id"] = int.Parse(item.Id.ToString());
                    dr["Name"] = item.Name.ToString();
                    dr["type_id"] = int.Parse(item.type_id.ToString());
                    if (item.Ownername != null)
                    {
                        dr["Ownername"] = item.Ownername.ToString();
                    }
                    if (item.Ownermob != null)
                    {
                        dr["Ownermob"] = item.Ownermob.ToString();
                    }
                    if (item.ContactPerson != null)
                    {
                        dr["ContactPerson"] = item.ContactPerson.ToString();
                    }
                    if (item.ContactPersonmob != null)
                    {
                        dr["ContactPersonmob"] = item.ContactPersonmob.ToString();
                    }
                    if (item.Phone != null)
                    {
                        dr["Phone"] = item.Phone.ToString();
                    }
                    if (item.Mobile != null)
                    {
                        dr["Mobile"] = item.Mobile.ToString();
                    }
                    if (item.Room != null)
                    {
                        dr["Room"] = item.Room.ToString();
                    }
                    if (item.Build != null)
                    {
                        dr["Build"] = item.Ownermob.ToString();
                    }
                    if (item.Road != null)
                    {
                        dr["Road"] = item.Road.ToString();
                    }
                    if (item.Area != null)
                    {
                        dr["Area"] = item.Area.ToString();
                    }
                    if (item.City != null)
                    {
                        dr["City"] = item.City.ToString();
                    }
                    if(item.country_id!=null)
                    {
                        dr["country_id"] = int.Parse(item.country_id.ToString());
                    }
                    if (item.state_id != null)
                    {
                        dr["state_id"] = int.Parse(item.state_id.ToString());
                    }
                    if (item.Phone != null)
                    {
                        dr["Phone"] = item.Phone.ToString();
                    }
                    if (item.Mobile != null)
                    {
                        dr["Mobile"] = item.Mobile.ToString();
                    }
                    if (item.Email != null)
                    {
                        dr["Email"] = item.Email.ToString();
                    }
                    if (item.Remark != null)
                    {
                        dr["Remark"] = item.Remark.ToString();
                    }
                    if(item.UserOFC!=null)
                    {
                        dr["UserOFC"] = bool.Parse(item.UserOFC.ToString());
                    }
                    if(item.UserVAT!=null)
                    {
                        dr["UserVAT"] = bool.Parse(item.UserVAT.ToString());
                    }
                    if(item.UserINV!=null)
                    {
                        dr["UserINV"] = bool.Parse(item.UserINV.ToString());
                    }
                    if(item.UserNone!=null)
                    {
                        dr["UserNone"] = bool.Parse(item.UserNone.ToString());
                    }
                    dr["Username"] = item.Username.ToString();
                    dr["Register_Date"] = Convert.ToDateTime(item.Register_Date.ToString());
                    dt.Rows.Add(dr);
                }
            }
            myDataSet.Tables.Add(dt);
            if (count > 0)
            {
                DataView myDataView = new DataView();
                myDataView = myDataSet.Tables[0].DefaultView;
                if (this.ViewState["SortExp"] != null)
                {
                    myDataView.Sort = this.ViewState["SortExp"].ToString()
                             + " " + this.ViewState["SortOrder"].ToString();
                }
                GV_ViewCustomers.DataSource = myDataView;
            }
            else
            {
                myDataSet.Tables[0].Rows.Add(myDataSet.Tables[0].NewRow());
                GV_ViewCustomers.DataSource = myDataSet;
                GV_ViewCustomers.DataBind();
                int columncount = GV_ViewCustomers.Rows[0].Cells.Count;
                GV_ViewCustomers.Rows[0].Cells.Clear();
                GV_ViewCustomers.Rows[0].Cells.Add(new TableCell());
                GV_ViewCustomers.Rows[0].Cells[0].ColumnSpan = columncount;
                GV_ViewCustomers.Rows[0].Cells[0].Text = "No Records Found";
            }
            GV_ViewCustomers.DataBind();
            if (GV_ViewCustomers.Rows.Count != 0)
            {
                SetPageNumbers();
            }
        }
    }

this is my grid view bind code. 这是我的网格视图绑定代码。

If UserOFC always contains either True or False then you can use the following approaach, 如果UserOFC始终包含TrueFalse那么您可以使用以下方法,

Checked='<%# Convert.ToBolean(Eval("UserOFC").ToString()) %>'

OR 要么

If UserOFC may contain other that True or False (a null value) then you can use the following approach, 如果UserOFC可能包含TrueFalsenull值)以外的其他内容,则可以使用以下方法,

On RowDataBound even of the GridView use RowDataBound甚至是GridView使用

if (e.Row.RowType == DataControlRowType.DataRow)
{
   DataRowView rowView = (DataRowView)e.Row.DataItem;
   if(rowView["UserOFC"] != null)
   {
      try
      {
        (e.Row.FindControl("chk_UserOFC1") as CheckBox).Checked = Convert.ToBolean(rowView["UserOFC"].ToString());
      }
      catch
      {
      }
   }
}

尝试将复选框的选中属性设置为

Checked='<%# Convert.ToBoolean(Eval("UserOFC").ToString()) %>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM