简略代码:
select tmp,buf from incliddataGridView1.DataSource = ds.Tables[0].DefaultView;查询inclid表结果显示在dataGridView1
字段tmp存储的是1和2,显示在dataGridView1上时1为学生,2为老师,
有办法能在查询语句或者给dataGridView1赋值前判断tmp值来转换成学生或者老师?

解决方案 »

  1.   

    使用CellFormatting事件,可以在字段值显示时做转换
            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
                if (e != null)
                {
                    if (e.ColumnIndex == dataGridView1.Columns["tmp"].Index)
                    {
                        string value = dataGridView1.Rows[e.RowIndex].Cells["tmp"].Value.ToString();
                        if (value == "1")
                        {
                            e.Value = "学生";
                        }
                        else
                        {
                            e.Value = "教师";
                        }
                        e.FormattingApplied = true;
                    }
                }
      

  2.   

    在数据库里面加一个表,比如叫TableA,里面内容如下a b
    1 学生
    2 老师查询语句这样写select b.b,a.buf from inclid a, tablea b where a.tmp=b.a
      

  3.   

    其实可以从SQL上进行解决的,sql case一下就成了~
      

  4.   

    case tmp when 1 then '学生' else '教师' end
      

  5.   

    select tmp,tmpName = case when tmp = 1 then '学生' when tmp = 2 then '老师' else NULL end ,buf from inclid