读书人

DataTable的值某列值依据判断后另外一

发布时间: 2012-11-11 10:07:57 作者: rapoo

DataTable的值某列值根据判断后另外一种显示形式绑定到DataGridView中
我有一张学生表里面有个性别的字段是Bit类型,我将该表的数据查询出来绑定到DataGridView中,但是Bit类型的那列数据我想0在DataGridView中就显示女,1就显示男,这个该怎么写啊,我看到一些网上数用DataTable的Select方法,但是我不知道怎么写。

[解决办法]
从SQL语句着手,使用case when处理

select case Sex when 0 then '女' else '男' end as Sex,Name,Age from Student;
[解决办法]
最近怎么DataGridView的问题这么多?

用 CellFormat 事件处理。

C# code
private void DataGridView1_CellFormatting(object sender,    DataGridViewCellFormattingEventArgs e){    DataGridView dgv = (DataGridView)sender;    // 如果单元格是“Column1”列的单元格    if (dgv.Columns[e.ColumnIndex].Name == "Column1" )    {                e.Value = e.Value == 0 ? "男" : "女";        e.FormattingApplied = true;    }} 

读书人网 >C#

热点推荐