我有网格视图,我将这个复选框绑定如下:
<ItemTemplate>
<asp:CheckBox ID="chk_UserOFC1" runat="server" ToolTip="User of Easy Office" Checked='<%#Eval("UserOFC")%>'/>
</ItemTemplate>
该字段是否为可空位,因此发生运行时错误:
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
---------------------------更新---------------- --------------------
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();
}
}
}
这是我的网格视图绑定代码。
答案 0 :(得分:3)
如果UserOFC
始终包含True
或False
,那么您可以使用以下方法,
Checked='<%# Convert.ToBolean(Eval("UserOFC").ToString()) %>'
或强>
如果UserOFC
可能包含其他True
或False
(null
值),那么您可以使用以下方法,
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
{
}
}
}
答案 1 :(得分:1)
尝试将checkbox的checked属性设置为
Checked='<%# Convert.ToBoolean(Eval("UserOFC").ToString()) %>'