C# 无法将“System.Data.DataRowView”类型的对象转换为“System.IConvertible”类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9429139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'
提问by forum
I m using in C# Code
我在 C# 代码中使用
int i = Convert.ToInt32(ddlDivisionId.SelectedValue);
at that time this type of error accrued..
当时产生了这种类型的错误..
Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'
how can i solve it ?
我该如何解决?
Please help
请帮忙
回答by Chris
It looks like your ddlDivisionId.SelectedValueis returning a DataRowView. I assume you bound a DataTable or similar to your dropdown list (assuming that is what we are looking at).
看起来您ddlDivisionId.SelectedValue正在返回一个DataRowView. 我假设您绑定了一个 DataTable 或类似于您的下拉列表(假设这是我们正在查看的)。
In this case you will need to treat the ddlDivisionId.SelectedValueas a DataRowView (probably casting to that object first) to get the value out of it... I assume something like:
在这种情况下,您需要将ddlDivisionId.SelectedValue视为 DataRowView (可能首先转换为该对象)以从中获取值......我假设如下:
int i = Convert.ToInt32(((DataRowView)ddlDivisionId.SelectedValue)["id"]);
Here you should replace "id" with whatever the name of your field is in your datatable that you want to get out as an integer.
在这里,您应该将“id”替换为您想要作为整数输出的数据表中的任何字段名称。
回答by malik saifullah
int i = Convert.ToInt32(ddlDivisionId.SelectedValue.ToString());
add .ToString in last it will work
最后添加 .ToString 它将起作用

