soonh
soonh

Reputation: 39

How to display some column in grid view?

I want display specific column in data grid view for profile of user after log in the system but my problem it is display all columns in C# with asp.net sql server, any one can help me?

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();
            }

        }
    }
}

Upvotes: 3

Views: 2376

Answers (3)

ArunPratap
ArunPratap

Reputation: 5020

Add OnRowDataBound of your GridView

<asp:GridView ID="GridViewID" runat="server" OnRowDataBound="gvTest_RowDataBound" >

in RowDataBound

First, check the roles of user and then you can check the condition and hide your column

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;
    }                                                                                                                                     
  }

Upvotes: 1

Nayan
Nayan

Reputation: 94

Make sure you have specified the Datasource property of column as database column name

Upvotes: 1

Mustafa Tığ
Mustafa Tığ

Reputation: 101

Do you want this like?

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[index].Visible = false;
}

You can check this answer.

GridView Hide Column by code

Upvotes: 1

Related Questions