无法转换类型为“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。
我搜索了解决方案并没有找到,所以我找到了并发布了我的解决方案。谢谢。
I will post an answer to an error I had since I found the solution.
I received the error in asp.net: Unable to cast object of type 'System.Data.DataRowView' to type '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
...
The Dataset returned a DataRowView and the line that caused the problem expected a DataRow.
I searched for the solution and didn't find, so I found it and posted my solution. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当数据绑定到 DataTable 时,数据绑定引擎将调用 DataTable 的
IListSource
实现并绑定到其DataView
。由于 DataView 保存 DataRowView 对象而不是 DataRow,因此您无法将绑定对象直接转换为 DataRow。
相反,您需要转换为
DataRowView
,然后获取Row
属性。有关发生这种情况的更详细说明,请参阅我的博客。
When databinding to a DataTable, the databinding engine will call the DataTable's
IListSource
implementation and bind to itsDataView
.Since DataViews hold DataRowView objects and not DataRows, you cannot cast the bound objects directly to DataRow.
Instead, you need to cast to
DataRowView
, then get theRow
property.For a more detailed explanation of why this happens, see my blog.
使用动态数据。它填充被填充属性。
use dynamic data . it fill be filled properties.