DataGridView中列绑定性别问题,求教! 数据库中DATABLE的性别列是数字表示,0为男,1为女现在我的winform的DataGridView 要绑定这个DATABLE,但性别列要自动显示 男和女! 怎么办?求完整代码方案 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select case sex when 0 then '男' when 1 then '女' end as sex from table然后绑定~ 可以自定义CellFormatting。如果需要编辑,则还要自定义CellParsing。 public Form1() { InitializeComponent(); DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Gender", typeof(int)); table.Rows.Add("Alice", 0); table.Rows.Add("Bob", 1); this.dataGridView1.DataSource = table; this.dataGridView1.Columns["Gender"].ReadOnly = true; this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting); } void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Gender" && e.Value is int) { e.Value = (int)e.Value == 0 ? "女" : "男"; e.FormattingApplied = true; } } data.Columns[2].Expression = "IIF(Column2='0', '男', '女')";private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if(this.dataGridView1.Columns[e.ColumnIndex].HeaderText =="sex") { if(object.Equals(e.Value,0)) { e.Value=object.Equals(e.Value,0)?"男 ":"女 "; } } } 关于记录游戏人物移动的坐标 读取文件时出现的怪问题 C# 2005 express edition beta1的一个bug,不敢肯定,大伙看看。 DataGrid里删除多行里有问题~ datagrid问题! 新手问题!各位高手请教教我!!! C# 动态给数据库添加字段 在.net 4.0 的工程中使用3.5的控件 C# DataGridview调用基类中的DataGridViewBind方法来绑定数据库失败怎么解决 请SQL高手看看一个很有技术含量、但又简单的SQL查询语句,不胜感激! 数据库操作类的问题 在C# 绑定datasourse问题?
然后绑定~
{
InitializeComponent(); DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Gender", typeof(int));
table.Rows.Add("Alice", 0);
table.Rows.Add("Bob", 1); this.dataGridView1.DataSource = table;
this.dataGridView1.Columns["Gender"].ReadOnly = true;
this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
} void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Gender" && e.Value is int)
{
e.Value = (int)e.Value == 0 ? "女" : "男";
e.FormattingApplied = true;
}
}
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(this.dataGridView1.Columns[e.ColumnIndex].HeaderText =="sex")
{
if(object.Equals(e.Value,0))
{
e.Value=object.Equals(e.Value,0)?"男 ":"女 ";
}
}
}