PDA

Click to See Complete Forum and Search --> : [1.0/1.1] Hiding columns in a datagrid


steve_rm
Nov 10th, 2007, 12:31 PM
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

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

r0k3t
Nov 10th, 2007, 01:17 PM
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...


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.