Results 1 to 9 of 9

Thread: [RESOLVED] Hide columns in Datagrid

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [RESOLVED] Hide columns in Datagrid

    Hello,

    VS 2005 WM 6.0

    I am using the following code to hide the grid columns. As I have a lot of grid columns to hide, it is showing a thick bold line where the grid column would be. I wanted to make the columns totally invisble by setting the width to 0. However, I am wondering if there is a better method as the customer doesn't like having the thick bold line, as you can see there are some there.

    Here is my code for hiding, is there something better.
    Code:
     //GridTableStylesCollection grdTblSty = new GridTableStylesCollection();
                    DataGridTableStyle tblStyle = new DataGridTableStyle();
                    tblStyle.MappingName = "OrderDetail";
                    grdBeverages.TableStyles.Add(tblStyle);
    
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["OrderDetailID"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["BeverageID"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["Beverage"].Width = 50;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["BeverageQty"].Width = 40;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["BeverageQty"].HeaderText = "Qty";
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["BeverageCost"].Width = 40;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["BeverageCost"].HeaderText = "Cost";
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["StarterID"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["Starter"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["StarterQty"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["StarterCost"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["MaincourseID"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["Maincourse"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["MaincourseQty"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["MaincourseCost"].Width = 0;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["TotalCost"].Width = 40;
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["totalCost"].HeaderText = "Cost";
                    grdBeverages.TableStyles["OrderDetail"].GridColumnStyles["OrderID"].Width = 0;
    Many thanks for any advice,

    Steve
    steve

  2. #2
    Junior Member gRob_AndDerSon's Avatar
    Join Date
    Jun 2007
    Location
    Philippines
    Posts
    31

    Re: Hide columns in Datagrid

    Filter the query results of your data table to exclude columns who are not IDs.
    VB.Net Version : 2005
    .Net Version : 2.0
    OS : Windows XP SP2

    -------------------------
    ALways be COOL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Hide columns in Datagrid

    Hello,

    Thanks for your suggestion, but that wouldn't work. I need all the columns but don't want to display some.

    I am still looking for a suitable solution to this problem.

    Many thanks for any more suggestions on this problem.

    Steve
    steve

  4. #4
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Hide columns in Datagrid

    Hi,
    try setting the column width of the columns you don't want to see, to zero

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Hide columns in Datagrid

    Hello Pete.

    Thanks for your reply, yes that is what I did in my first post.

    However, there was a black bold line where the column should be. The customer didn't like to see this. As you can see there are many columns I need to hide.

    Thanks for any more suggestions,

    Steve
    steve

  6. #6
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Hide columns in Datagrid

    Hi,
    sorry - didn't read it properly. What about if you put all the columns you didn't want to see at the far right.
    Do you use a dataset to populate your grid - if so, you could try
    dsGeneral.Tables("General").Columns("Product_Name").ColumnMapping = MappingType.Hidden, and this has another possibility

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Hide columns in Datagrid

    Hello Pete,

    Thanks for your continued support.

    This didn't work.
    dsGeneral.Tables("General").Columns("Product_Name").ColumnMapping = MappingType.Hidden

    I need to test the other website, but it doesn't look too promising.

    Thanks if you have any other ideas.

    Steve
    steve

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Hide columns in Datagrid

    Hello,

    I have found some code that seems to work. However, the idea of the grid is for the user to keep adding new products to the datagrid. Even though the columns that are needed are added ok. When the user attempts to add a second row there is an error. ""Value does not fall within the expected range". This happens each time, after the first one has been successfully added. 2nd one onwards.

    I thought it might be the tblStyle object, but as I am creating a new one each time, I am surprised to get this error.

    vb Code:
    1. DataGridTableStyle tblStyle = new DataGridTableStyle();
    2.                 tblStyle.MappingName = "OrderDetail";
    3.  
    4.                 DataGridTextBoxColumn column = new DataGridTextBoxColumn();
    5.                 column.MappingName = "Beverage";
    6.                 column.HeaderText = "Beverage";
    7.                 tblStyle.GridColumnStyles.Add(column);
    8.  
    9.                 column = new DataGridTextBoxColumn();
    10.                 column.MappingName = "BeverageQty";
    11.                 column.HeaderText = "Quantity";
    12.                 tblStyle.GridColumnStyles.Add(column);
    13.  
    14.                 grdBeverages.TableStyles.Add(tblStyle); //Error - "Value does not fall within the expected range"
    15.                 grdBeverages.DataSource = DS.OrderDetail.DefaultView;

    Many thanks if you have any ideas about this,

    Steve
    steve

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Hide columns in Datagrid

    Problem solved,

    You cannot have a second table style.

    vb Code:
    1. grdBeverages.TableStyles.Clear()
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width