日期:2014-05-17  浏览次数:20784 次

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;
    }
}