WinForm的DataGridView控件在DataSource绑定一个DataTable表后修改cell值的有关问题
WinForm的DataGridView控件在DataSource绑定一个DataTable表后修改cell值的问题
我数据库表中有一列性别是int类型 存放数值 1 0 也就是男或女
从数据库将这个表读出来 放入DataTable中
使用DataSource=DataTable这个表
这列的类型是 DataGridViewTextBoxColumn
绑定完后 我修改这列的 DataGridView.Rows[i].Cells[0].Value = "男";
这样会报错 “男”不是有效的int32值
我将DataGridView.Rows[i].Cells[0].DataType = typeof(string);后 也是不行的
我查看DataGridView.Rows[i].Cells[0].Value的类型是 object{int}类型
我怎么能将string类型赋值给这个 DataGridViewTextBoxCell呢???
------解决方案--------------------
我数据库表中有一列性别是int类型 存放数值 1 0 也就是男或女
从数据库将这个表读出来 放入DataTable中
使用DataSource=DataTable这个表
这列的类型是 DataGridViewTextBoxColumn
绑定完后 我修改这列的 DataGridView.Rows[i].Cells[0].Value = "男";
这样会报错 “男”不是有效的int32值
我将DataGridView.Rows[i].Cells[0].DataType = typeof(string);后 也是不行的
我查看DataGridView.Rows[i].Cells[0].Value的类型是 object{int}类型
我怎么能将string类型赋值给这个 DataGridViewTextBoxCell呢???
------解决方案--------------------
private void dgvPatientDocInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow row = this.dgvPatientDocInfo.Rows[e.RowIndex];
if (e.ColumnIndex == 2)
{
if (row != null)
{
e.value=row.Cells["sex"].Value.ToString()=="1"?"男":"女";
}
}
}