【发布时间】:2014-04-14 09:06:01
【问题描述】:
我有一个 GridView,我像这样分配 DataSource 和 Bind:
protected void Search(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(constring))
using(SqlCommand com = new SqlCommand("XXX", con))
{
com.CommandType = CommandType.StoredProcedure;
// parameter declarations are here
con.Open();
SqlDataReader rdr = com.ExecuteReader();
gvResults.DataSource = rdr;
gvResults.DataBind();
}
}
然后有一个 OnRowBound 方法,如下所示:
protected void gvResults_OnRowBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType != DataControlRowType.DataRow) return;
DataRowView rowdata = (DataRowView)row.DataItem;
// fancy stuff will happen here
}
当到达尝试将行的 DataItem 转换为 DataRowView 的行时,会引发错误,内容如下:
无法将“System.Data.Common.DataRecordInternal”类型的对象转换为“System.Data.DataRowView”类型
我知道我显然没有将此 DataItem 转换为正确的类,但我的问题是当 DataSource 是 SqlDataReader 时,要转换的正确类是什么? p>
还是我完全走错了路?
【问题讨论】:
标签: c# asp.net gridview casting ado.net