Results 1 to 2 of 2

Thread: [1.0/1.1] Hiding columns in a datagrid

  1. #1

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

    [1.0/1.1] Hiding columns in a datagrid

    Hello

    I am trying to hide some columns in a datagrid. The datagrid is bound to a typed dataset data source.

    This is the code I have used to set the column width to zero. However, as there are many columns to hide I get left with a bold black line where the column should have been. So the customer doesn't want to see this. This is the code I used, just wondering is there a better way to do this.

    Many thanks,

    Steve

    Code:
    //GridTableStylesCollection grdTblSty = new GridTableStylesCollection();
                    DataGridTableStyle tblStyle = new DataGridTableStyle();
                    tblStyle.MappingName = "OrderDetail";
                    grdBeverages.TableStyles.Add(tblStyle);
    
                    grdBeverages.DataSource = DS.OrderDetail.DefaultView;
                    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;
    steve

  2. #2
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: [1.0/1.1] Hiding columns in a datagrid

    Well since you are setting the datasource in the code behind page why not just create your own datatable? I find this solution to be quite useful in a lot of cases.

    here is an example...

    Code:
    SqlConnection sqlConn = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = sqlConn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "GetAllTopicsInHeader";
    cmd.Parameters.AddWithValue("@Header", System.Data.OleDb.OleDbType.VarChar).Value = topicHeader;
    SqlDataReader dr;
    //this is where we create the datatable that we are going to
    //assign assign as the datasource for the dataGridView control or
    //whatever it is...     
    DataTable table = new DataTable();
    table.Columns.Add("Header", typeof(string));
    table.Columns.Add("title", typeof(string));
    table.Columns.Add("htmlUrl", typeof(string));
    try
    {
        sqlConn.Open();
        //notice we don't even need a DataSet, a reader will do just fine.  
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            //cruz through the reader and stick our data in the table... You have to 
            //be careful of data types here! Make sure they match the columns... 
            //Usually just make everything a string! 
            table.Rows.Add(new object[] { dr["Header"], dr["title"], dr["htmlUrl"] });
        }
        sqlConn.Close();
    }
    catch (Exception ex)
    {
         sqlErrorLabel.Text = ex.ToString();
         sqlConn.Close();
    }
    
    //return the table and assign it as the data source...
    return table;
    Now, what I am assuming is that you want the data that you aren't displaying for other reasons - otherwise just don't get it at all, then you don't have to worry about hiding it! Anyway - there are a lot ways to do what you want... This is just a method I find I use when I run into situations when I have to get all kinds of data and some of it needs to be displayed.

    Hope this helps.

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