无法在数据绑定(gridview)中将类型'string'隐式转换为'bool'

时间:2013-02-20 15:04:55

标签: c# casting

这是将String分配给布尔属性的行:

chkreputative.Checked = gvmanufacturers.DataKeys[rowindex]["IsReputative"].ToString();

1 个答案:

答案 0 :(得分:2)

摆脱ToString()电话,并应用正确的演员表。您正尝试为String值分配Boolean值,这就是您获得例外的原因。

根据DataKeys对象的类型,您可以尝试以下某些操作:

chkreputative.Checked = (bool)gvmanufacturers.DataKeys[rowindex]["IsReputative"];

chkreputative.Checked = Boolean.Parse(gvmanufacturers.DataKeys[rowindex]["IsReputative"]);

chkreputative.Checked = Convert.ToBoolean(gvmanufacturers.DataKeys[rowindex]["IsReputative"]);