我使用SQL query
来定义表中用户的角色。例如: -
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>
答案 0 :(得分:0)
在Grid事件RowDatabound
下,你必须处理这个或你的SQL查询将返回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>