Unable to cast object of type 'System.Data.DataRowView' to type 'System.IConvertible'

不打扰是莪最后的温柔 提交于 2021-02-07 08:35:31

问题


I m using in C# Code

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


回答1:


It looks like your ddlDivisionId.SelectedValue is returning a DataRowView. I assume you bound a DataTable or similar to your dropdown list (assuming that is what we are looking at).

In this case you will need to treat the ddlDivisionId.SelectedValue as a DataRowView (probably casting to that object first) to get the value out of it... I assume something like:

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.




回答2:


int i = Convert.ToInt32(ddlDivisionId.SelectedValue.ToString());

add .ToString in last it will work



来源:https://stackoverflow.com/questions/9429139/unable-to-cast-object-of-type-system-data-datarowview-to-type-system-iconvert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!