Hiding GridView columns by user

Last post 01-23-2008 2:29 PM by KelseyThornton. 7 replies.

Sort Posts:

  • Hiding GridView columns by user

    01-18-2008, 1:53 AM
    • Loading...
    • amardilo
    • Joined on 09-04-2006, 10:55 AM
    • Posts 79

    Hi there.

    I have a GridView made up of data from 1 database (GridView includes Ccheck boxes, text boxes, labels, links and coloured rows) and I have data from another database that holds preferences as to what each each user would like to see (i.e user a does not need to see column x on the GridView). All data needs to be  on the GridView (for  behind the screen reasons) but some users do not need to see those columns on screen (if it helps both sets of data will be binded to the GridView by custom collections/entities/classes/data types).

    Does anyone know how to do this? 

  • Re: Hiding GridView columns by user

    01-18-2008, 4:00 AM

    lets say u have a list of column that needs to be invisible. Loop through all the ciolumn of the gridview and make thods column invisible. DO this after you have called the databind fro the gridview. here is the sudo code for the same

    for(int i=0;i

    Vikram
    www.vikramlakhotia.com
    justlikethat.vikramlakhotia

    Please mark the answer if it helped you
  • Re: Hiding GridView columns by user

    01-18-2008, 4:29 AM
    Answer
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,123

    Here my idea

    protected void Page_Load(object sender, EventArgs e)
    {
      
          //First create a query that will fetch the user from the databased based on the user who Logs in       
          //Something like "SELECT userid From Table Where username = @userwhologsin "
       
        DataTable dtuser = //set datasource here based on the query
        String user = dtuser[0]["UsernameField"].ToString();

       
         //Next Fetch all the data fron the database
         DataTable dt = //Set DataSource here
         GridView1.DataSource = dt;
         GridView1.DataBind();

         For (int i = 0; i < dt.Rows.Count; i++)
         {
          if (user == userwhologsin)
          {
            //lets say we want to hide column 2
            GridView1.Columns[1].Visible = false;
          }
         }
    }

    Hope my suggestion helps 

    Cheers,
    Vincent Maverick Durano


  • Re: Hiding GridView columns by user

    01-18-2008, 3:37 PM
    • Loading...
    • amardilo
    • Joined on 09-04-2006, 10:55 AM
    • Posts 79

    Thanks for those examples dudes!

    I use and need to hide a lot of TemplateField / ItemTemplate columns would this still work?
     

  • Re: Hiding GridView columns by user

    01-18-2008, 5:06 PM
    Answer
    • Loading...
    • KelseyThornton
    • Joined on 07-15-2007, 8:01 AM
    • Breda, The Netherlands
    • Posts 199

    There's no reason I can think of why it should not work with templatefields - This method simply disables the column in the grid.

    See also http://forums.asp.net/t/1158847.aspx and http://forums.asp.net/t/1170046.aspx

    Kelsey Thornton
    (In the Netherlands)

    Don't forget - Mark the post which answered your question with "Answer", then that user will get some kudos, and your post will be marked as "Answered" for future readers!
  • Re: Hiding GridView columns by user

    01-21-2008, 7:18 AM
    • Loading...
    • vinz
    • Joined on 10-05-2007, 11:47 AM
    • Cebu Philippines
    • Posts 6,123

    Yes you can hide the templatefields in the grid by just Hiding the Column in which your TemplateFields resides

     

    Cheers,
    Vincent Maverick Durano


  • Re: Hiding GridView columns by user

    01-23-2008, 2:22 PM
    • Loading...
    • amardilo
    • Joined on 09-04-2006, 10:55 AM
    • Posts 79

    Cool thanks for the help guys.

    Does that mean hiding the entire template field control? If I use those code examples will they also hide the header text?
     

  • Re: Hiding GridView columns by user

    01-23-2008, 2:29 PM
    • Loading...
    • KelseyThornton
    • Joined on 07-15-2007, 8:01 AM
    • Breda, The Netherlands
    • Posts 199

    Absolutely *everything* disappears...

    Imagine taking a WORD table and deleting a column - the column simply isn't there any more.

    this method does have a disadvantage, however.  None of the controls in the "hidden"  column is available - this also implies that the column simply *isn't there*.

    Kelsey Thornton
    (In the Netherlands)

    Don't forget - Mark the post which answered your question with "Answer", then that user will get some kudos, and your post will be marked as "Answered" for future readers!
Page 1 of 1 (8 items)