SQL 用户类型问题
SQL usertype Issue
我已经使用 SQL query
来定义 table 中用户的角色。例如:-
1 -- 超级管理员
2 -- 管理员
3 -- 用户
在 gridview 中,我想将文本显示为 Super Admin
、Admins
,但我仍然得到 0
、1
和 2
。
请帮忙
代码:-
SELECT (
CASE WHEN usertype =1 THEN 'Admin'
WHEN usertype=0 THEN 'Super Admin'
WHEN usertype=2 THEN 'User' END ) aS usertype
FROM tbl_User
Gridview aspx代码:-
<asp:GridView ID="grdUser" AllowPaging="true" AutoGenerateColumns="False"
OnDataBound="grdUser_DataBound" OnRowDeleting="grdUser_RowDeleting"
OnPreRender="PreRenderGrid" runat="server" Width="100%"
border="1" DataKeyNames="Id" PageSize="2"
OnPageIndexChanging="grdUser_PageIndexChanging"
EnableSortingAndPagingCallbacks="false"
CssClass="pagi" OnRowCommand="grdUser_RowCommand">
<Columns>
<asp:BoundField DataField="username" HeaderText="UserName"
ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<HeaderStyle CssClass="k-grid td"></HeaderStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="usertype" HeaderText="UserType"
ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<HeaderStyle CssClass="k-grid td"></HeaderStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
在 Grid 事件 RowDatabound
下,您必须处理这个,否则您的 SQL 查询将 return RoleDescription 和 roleID,然后将其绑定到 Grid
对于 RowDataBound 事件:-
protected void ctrlGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
//add three case like this
if (dataBoundItem["usertype"].Text == "1")
Label lblUserType = (dataBoundItem["UserType"].FindControl("lblUserType") as Label);
lblUserType.Text = "Admin";
}
编辑:-
根据您的 sql 查询,我必须说更改您的别名并添加单引号
SELECT Id,username,email,usertype,active,
CASE WHEN usertype ='1' THEN 'Admin'
WHEN usertype='0' THEN 'Super Admin'
WHEN usertype='2' THEN 'User' END ) AS usertypeDescription
from tbl_User ORDER By Id DESC
然后改变
<asp:BoundField DataField="usertypeDescription" HeaderText="UserType"
ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<HeaderStyle CssClass="k-grid td"></HeaderStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
我已经使用 SQL query
来定义 table 中用户的角色。例如:-
1 -- 超级管理员
2 -- 管理员
3 -- 用户
在 gridview 中,我想将文本显示为 Super Admin
、Admins
,但我仍然得到 0
、1
和 2
。
请帮忙
代码:-
SELECT (
CASE WHEN usertype =1 THEN 'Admin'
WHEN usertype=0 THEN 'Super Admin'
WHEN usertype=2 THEN 'User' END ) aS usertype
FROM tbl_User
Gridview aspx代码:-
<asp:GridView ID="grdUser" AllowPaging="true" AutoGenerateColumns="False"
OnDataBound="grdUser_DataBound" OnRowDeleting="grdUser_RowDeleting"
OnPreRender="PreRenderGrid" runat="server" Width="100%"
border="1" DataKeyNames="Id" PageSize="2"
OnPageIndexChanging="grdUser_PageIndexChanging"
EnableSortingAndPagingCallbacks="false"
CssClass="pagi" OnRowCommand="grdUser_RowCommand">
<Columns>
<asp:BoundField DataField="username" HeaderText="UserName"
ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<HeaderStyle CssClass="k-grid td"></HeaderStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="usertype" HeaderText="UserType"
ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<HeaderStyle CssClass="k-grid td"></HeaderStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
在 Grid 事件 RowDatabound
下,您必须处理这个,否则您的 SQL 查询将 return RoleDescription 和 roleID,然后将其绑定到 Grid
对于 RowDataBound 事件:-
protected void ctrlGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
//add three case like this
if (dataBoundItem["usertype"].Text == "1")
Label lblUserType = (dataBoundItem["UserType"].FindControl("lblUserType") as Label);
lblUserType.Text = "Admin";
}
编辑:-
根据您的 sql 查询,我必须说更改您的别名并添加单引号
SELECT Id,username,email,usertype,active,
CASE WHEN usertype ='1' THEN 'Admin'
WHEN usertype='0' THEN 'Super Admin'
WHEN usertype='2' THEN 'User' END ) AS usertypeDescription
from tbl_User ORDER By Id DESC
然后改变
<asp:BoundField DataField="usertypeDescription" HeaderText="UserType"
ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
<HeaderStyle CssClass="k-grid td"></HeaderStyle>
<ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>