C#,datagrid属性给出强制转换异常 [英] C# ,datagrid property gives cast exception

查看:97
本文介绍了C#,datagrid属性给出强制转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据集,其中有7列,
我有一个数据网格,绑定到上述数据集,我有一个布尔列,其columnstyle是datagridviewcheckbox样式,我想将其设置为
datagridviewtextbox列样式,

因此,当布尔值显示在datagrid中而不是复选框时,我希望将true/false文本写为列值.

i have dataset, which has 7 columns,
i have a datagrid, binded to above dataset,i have a boolean column,whose columnstyle is datagridviewcheckbox style, i want to set it to
datagridviewtextboxcolumn style,

so that when boolean values are displayed in datagrid, instead of checkbox,i want true/false text to be written as column values.

DataSet ds = daoDistrict.FillDistrictGrid(Convert.ToInt32(cbStateName.SelectedValue));
dgDistrict.DataSource = ds.Tables[0];                        

DataGridViewTextBoxColumn txtBxCol = new DataGridViewTextBoxColumn();
            DataGridViewColumn tempCol = (DataGridViewColumn)dgDistrict.Columns[6];
            txtBxCol = (DataGridViewTextBoxColumn)tempCol;            
            txtBxCol.Name = "IsActive";
            txtBxCol.DataPropertyName = "IsActive";
            txtBxCol.HeaderText = "Is--Active";
            DataGridView dgv = txtBxCol.DataGridView;            
            bool val = txtBxCol.Displayed;
            int ind = txtBxCol.DisplayIndex;
            bool isDat = txtBxCol.IsDataBound;
            txtBxCol.ValueType = txtBxCol.GetType(); 



上面的代码将引发强制转换异常,无法将DGVCheckbxcol强制转换为DGVtextbxcol

在此先感谢



above code throws cast exception, unable to cast DGVCheckbxcol to DGVtextbxcol

thanks in advance

推荐答案

unable to cast DGVCheckbxcol to DGVtextbxcol



永远不能强制转换, DataGridViewTextBoxColumn 不同于DataGridViewCheckBoxColumn

试试



this can be never casted, DataGridViewTextBoxColumn is deferent from DataGridViewCheckBoxColumn

try

DataGridViewCheckBoxColumn txtBxCol = new DataGridViewCheckBoxColumn();
            DataGridViewColumn tempCol = (DataGridViewColumn)dgDistrict.Columns[6];
            txtBxCol = (DataGridViewCheckBoxColumn)tempCol; 


这篇关于C#,datagrid属性给出强制转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆