绑定数据源代码:
gridUserList.DataSource = SQLHelper.Query("select * from users order by is_avail desc");//Query函数返回的是DataSet;
  gridUserList.DataBind();
这里表users里有两个字段是用户类型(userType)和是否有效(is_avail),字段userType里面存的是1,2,3;1代表的是管理员,2代表用户,3代表负责人,字段is_avail里存的是0和1,0表示否,1表示是,我想在gridview里显示出来用户类型是汉字,如“管理员”,而不是“1”,请问代码怎么写

解决方案 »

  1.   

    SQL语句改下就可以了 
    select case userType when 1 then '管理员' when 2 then '用户' when 3 then '负责人' end as userType ,
    case is_avail when 0 then '否' when 1 then '是' end as is_avail
    from users 
    order by is_avail desc
      

  2.   

    select userType,case userType when 1 then '管理员' when 2 then '用户' when 3 then '负责人' end as userTypeName
    is_avail,case is_avail when 0 then '否' when then '是' end as is_availName
    from users 
    order by is_avail desc刚才的SQL写的不够完善,后面不能取到ID在你的datagridview上增加2列  userTypeName,is_availName  用来绑定汉字用把userType,is_avail列设置为隐藏,方便后面读取到ID
      

  3.   

    gridUserList 你有给他加列吗?  你可以再界面上先加好 或者在代码里面加异常发出来看看
      

  4.   

    加了,添加的列的名字是在DataField里写userTypeName和is_availname吧。异常是用户代码未处理  IErrorInfo.GetDescription 因 E_FAIL(0x80004005) 而失败。
      

  5.   


    //GridView的RowDataBound事件
    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "1")
                {
                    e.Row.Cells[0].Text = "管理员";
                }
                if (e.Row.Cells[0].Text == "2")
                {
                    e.Row.Cells[0].Text = "用户";
                }
                if (e.Row.Cells[0].Text == "3")
                {
                    e.Row.Cells[0].Text = "负责人";
                }
                if (e.Row.Cells[1].Text == "0")
                {
                    e.Row.Cells[1].Text = "否";
                }
                if (e.Row.Cells[1].Text == "1")
                {
                    e.Row.Cells[1].Text = "是";
                }
            }
        }
    3楼的方法也行,直接改SQL语句!
      

  6.   

    3楼的SQL语句在sql 数据库中可以用,但是在access里面提示语法错误(操作符丢失)在userType when 1 then '管理员' when 2 then '用户' when 3 then '负责人' end as userTypeName ,is_avail,case is_avail when 0 then '否' when then '是' end as is_availName处错误,在表里地段userType的类型是文本,字段is_avail是数字,这个sql语句就不知道怎么写了
     8楼的行。
      

  7.   

    Access数据库的话 把2个as去掉应该就可以了,好像Access数据库不支持这个AS关键字
    select userType,case userType when 1 then '管理员' when 2 then '用户' when 3 then '负责人' end userTypeName
        is_avail,case is_avail when 0 then '否' when then '是' end  is_availName
    from users 
    order by is_avail desc