我希望在登录系统后在数据网格视图中显示特定列的用户配置文件,但我的问题是在C#中使用asp.net sql server显示所有列,任何人都可以帮助我吗?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["MyID"] != null)
{
if (!IsPostBack)
{
string Id1 = Request.QueryString["MyID"];
dt = d.userunfo(Id1);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
你想要这个吗?
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
你可以查看这个答案。
确保已将column的Datasource属性指定为数据库列名
添加你的OnRowDataBound
的GridView
<asp:GridView ID="GridViewID" runat="server" OnRowDataBound="gvTest_RowDataBound" >
在RowDataBound
首先,检查用户的角色,然后您可以检查条件并隐藏列
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check User Type
int userType = //Your User Type Here;
if (userType == 1)
{
GridViewID.Columns[15].Visible = false;
}
else if (userType == 2 || userType == 3)
{
GridViewID.Columns[5].Visible = false;
GridViewID.Columns[6].Visible = false;
}
}