【发布时间】:2015-06-18 10:20:01
【问题描述】:
我正在使用 GridView 来显示一个报告,该报告显示来自数据库中不同表的信息。此报告中的一列显示从数据库中的维护表中获取的 checklistNo。我正在尝试将清单设为无链接,因此单击它时会打开清单页面。
protected void gvResults_OnItemDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
lookupChecklist main = (lookupChecklist)e.Row.DataItem;
Maintenance maint = (Maintenance)e.Row.DataItem;
GridView gv = (GridView)sender;
Literal litChecklistNo = e.Row.FindControl("litChecklistNo") as Literal;
if (maint.ChecklistID.HasValue)
{
switch (maint.VehicleTrailer)
{
case "Truck":
{
litChecklistNo.Text = "<a href=\"#\" onclick=\"OpenNew('/lookups/Vehicle.aspx?VehicleID=" + maint.LinkedID.ToString() + "&ChecklistNo=" + maint.CheckListNo + "', 'Vehicle');\">" + maint.CheckListNo + "</a>";
break;
}
case "Trailer":
{
litChecklistNo.Text = "<a href=\"#\" onclick=\"OpenNew('/lookups/Trailer.aspx?TrailerID=" + maint.LinkedID.ToString() + "&ChecklistNo=" + maint.CheckListNo + "', 'Trailer');\">" + maint.CheckListNo + "</a>";
break;
}
case "MSQ":
default:
{
break;
}
}
}
}
由于我需要的信息来自维护表,因此我添加了Maintenance maint = (Maintenance)e.Row.DataItem;,但这给出了错误
无法将“BaseClasses.lookupChecklist”类型的对象转换为类型 'BaseClasses.Maintenance'。
我不能从另一个班级调用维护班吗?
public static List<lookupChecklist> SearchCheckListItems(int CompanyID,
string[] SearchTerms,
string[] SearchFieldKey,
string DateTypeKey,
int? ServiceTypeID,
DateTime? FromDate,
DateTime? ToDate,
string ResolveKey,
string MaintenanceKey
)
{
if (SearchTerms != null &&
SearchFieldKey != null &&
SearchTerms.Length > 0 &&
SearchFieldKey.Length > 0 &&
SearchTerms.Length != SearchFieldKey.Length)
throw new Exception("Search Error: Search Terms must equal Search fields");
List<MySqlParameter> param = new List<MySqlParameter>{ new MySqlParameter("compid", CompanyID) };
StringBuilder SQL = new StringBuilder(SearchSQL);
List<lookupChecklist> main = new List<lookupChecklist>();
DataTable dtRes = lookupChecklist.CustomFill(SQL.ToString(), param);
foreach (DataRow dr in dtRes.Rows)
{
main.Add(new lookupChecklist(dr));
}
return main;
}
【问题讨论】:
-
在我看来,您正在将 lookupChecklists 绑定到 gridview,然后尝试将绑定的对象强制转换为“维护”类型……对吗?
-
@FullTimeSkeleton 网格视图没有绑定到任何东西
-
gridview 的数据源是什么?以及它如何转换一行来输入lookupChecklist,然后输入Maintenance?
-
@FullTimeSkeleton 我在我的问题中添加了 gridview 的代码
-
我很困惑,当我使用 Gridviews 时,你为它们提供了一个数据源,然后你可以将行转换为一个类型,一旦你知道源类型。鉴于此,我从未见过尝试转换为两种不同类型的 GV。如果删除此行:lookupChecklist main = (lookupChecklist)e.Row.DataItem;有用吗?
标签: c# html asp.net gridview unhandled-exception