日期:2014-05-19  浏览次数:20861 次

datagridview 中代码转换问题
请教以下,我在DATAGRIDVIEW中有有些性别代码为1或者是2,我想把他们转换成男,女,这个循环怎么写,大家帮个忙,写详细点,本人不胜感激

------解决方案--------------------
不如直接在数据库里写
update table set sex = "男 " where sex = "1 "
update table set sex = "女 " where sex = "2 "
------解决方案--------------------
可以取数据时case 性别字段 when '1 ' then '男 ' when '2 ' then '女 ' end as xxxx

如果绑定后转换,
for ()
if(ctype(dgitem.findcontrols( " "),label).text== '1 '))
{}
else
{}

------解决方案--------------------
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
GridView1.Rows[i].Cells[3].Text = "男 ";
GridView1.Rows[i].Cells[4].Text = "女 ";
}
其中Cells[3]或者Cells[4]是你要转换的列的位置.你要自己改一下
------解决方案--------------------
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].HeaderText == "sex ")
{
if (object.Equals(e.Value, 1))
{
e.Value = "男 ";
}
else if (object.Equals(e.Value, 0))
{
e.Value = "女 ";
}
}
}