3
的RowBound事件我有一個GridView,我分配這樣的數據源和綁定:錯誤鑄造SqlDataReader對象的DataItem到DataRowView的在GridView控件
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時,什麼纔是正確的類?
還是我在錯誤的軌道上完全?
謝謝.......它節省了我的時間 –