2011-01-12 79 views
2

我將發佈自我找到解決方案後發生的錯誤的答案。無法投射「System.Data.DataRowView」類型的對象來鍵入「System.Data.DataRow」

我收到在asp.net錯誤:無法投型 'System.Data.DataRowView' 的對象鍵入 '的System.Data.DataRow'

// Old line 
// rpOutils.DataSource = ds.Tables[0].Select("rnco_lang = '" + ddlLang.SelectedValue + "'"); 
// rpOutils.DataSource = ds; // New line that caused the error. I just wanted to pass a DataSet 
rpOutils.DataSource = ds.Tables[0].Select(); // New line with the solution. 
rpOutils.DataBind(); 

protected void rpOutils_ItemDataBound(object sender, RepeaterItemEventArgs e) 
     { 
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      { 
       DataRow row = (DataRow)e.Item.DataItem; // I received the System.InvalidCastException 

...

的數據集返回一個DataRowView,並導致問題的行預計DataRow。

我搜索瞭解決方案,並沒有找到,所以我找到了它,並張貼我的解決方案。謝謝。

+0

由於錯誤陳述:DataRowView row =(DataRowView)e.Item.DataItem; – Win 2011-08-05 20:48:35

回答

6

當數據綁定到DataTable時,數據綁定引擎將調用DataTable的IListSource實現並綁定到它的DataView

由於DataViews包含DataRowView對象而非DataRows,因此無法將綁定對象直接轉換爲DataRow。
相反,您需要投到DataRowView,然後獲取屬性。請參閱my blog

0

使用動態數據。它填充填充屬性。

​​
相關問題