Ask a questionAsk a question
 

AnswerHiding a Column in a SharePoint Webpart

  • Wednesday, November 04, 2009 4:16 PMChris.Tysh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    We are trying to change a custom made webpart that was built by our old consultant. The CSharp code calls a function that gets data from our MySQL database. The CSharp then uses .ToTable to put the MySQL Data into a Webpart table.

    My problem is coming up here - we have added a column to the MySQL data. This column is called ClientID. We do not want this column (Client ID) to show up in the Webpart's table, but we want to reference the column (Client ID) in a dropdown SPMenuField that appears for each row, we get an error - presumably because the Client ID is not in the table.

    Is there a way to hide the Client ID column from displaying, but still have it available for reference in the dropdown menu?

    Here is the current, non-working code:


    gvmain.DataSource = data.ToTable(true,"Alpha ID", "Company", "Status");
                
    SPMenuField menu = new SPMenuField();
    
    menu.HeaderText = "Alpha ID";
    menu.TextFields = "Alpha ID";
    menu.MenuTemplateId = "ClientListMenu";
    menu.NavigateUrlFields = "Alpha ID";
    menu.NavigateUrlFormat = "/clients/clientdetail.aspx?" + Common.Constants.QSClientID + "={0}&" + Common.Constants.QSPrjStatus + "=2";
    
    menu.SortExpression = "Sema ID";
    menu.TokenNameAndValueFields = "CLIENTID=Client ID";

    • Moved byDavid M MortonMVPWednesday, November 04, 2009 4:48 PMNot a C# Language question (From:Visual C# Language)
    •  

Answers

  • Wednesday, November 04, 2009 9:58 PMChris.Tysh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I set AutoGenerateColumnsFromDataTable = false and then manually added each of the columns. This still allowed me to access the Client ID data even though I did not add Client ID to the display columns.



    gvmain.AutoGenerateColumnsFromDataTable =

    false;

    ....

     

    SPBoundField status = new SPBoundField();
    status.HeaderText =
    "Status";
    status.DataField =
    "Status";
    status.SortExpression =
    "Status";
    gvmain.Columns.Add(status);

     

    SPBoundField type = new SPBoundField();
    type.HeaderText =
    "Type";
    type.DataField =
    "Type";
    type.SortExpression =
    "Type";
    gvmain.Columns.Add(type);

     

    SPBoundField name = new SPBoundField();
    name.HeaderText =
    "Name";
    name.DataField =
    "Name";
    name.SortExpression =
    "Name";
    gvmain.Columns.Add(name);

    • Marked As Answer byChris.Tysh Wednesday, November 04, 2009 9:58 PM
    •  

All Replies

  • Wednesday, November 04, 2009 4:45 PMChris.Tysh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Another note - If I change the last line in the above snipit to :

    menu.TokenNameAndValueFields = "CLIENTID=Alpha ID";
    The page then loads, but obviously it's not feeding the desired Client ID to the SPMenuField.
  • Wednesday, November 04, 2009 6:27 PMChris.Tysh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I am trying to use this line of code:

     

    this.gvmain.Columns[1].Visible = false;

     

     

    but I get an error saying "Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
     "

    Obviously Columns[1] is non-negative and less than the size of the collection, so... not sure what to do. 

    Any help would be greatly appreciated.

  • Wednesday, November 04, 2009 9:58 PMChris.Tysh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I set AutoGenerateColumnsFromDataTable = false and then manually added each of the columns. This still allowed me to access the Client ID data even though I did not add Client ID to the display columns.



    gvmain.AutoGenerateColumnsFromDataTable =

    false;

    ....

     

    SPBoundField status = new SPBoundField();
    status.HeaderText =
    "Status";
    status.DataField =
    "Status";
    status.SortExpression =
    "Status";
    gvmain.Columns.Add(status);

     

    SPBoundField type = new SPBoundField();
    type.HeaderText =
    "Type";
    type.DataField =
    "Type";
    type.SortExpression =
    "Type";
    gvmain.Columns.Add(type);

     

    SPBoundField name = new SPBoundField();
    name.HeaderText =
    "Name";
    name.DataField =
    "Name";
    name.SortExpression =
    "Name";
    gvmain.Columns.Add(name);

    • Marked As Answer byChris.Tysh Wednesday, November 04, 2009 9:58 PM
    •  
  • Thursday, November 05, 2009 4:15 PMChris.Tysh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I was also then able to add the column I needed and use

    gvmain.Columns[0].Visiblity = false;


    to hide the column from view.