绑定数据源代码:
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”,请问代码怎么写
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”,请问代码怎么写
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
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
//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语句!
8楼的行。
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