views:

113

answers:

2

I am using an ASP.NET GridView control, created dynamically with C# on a SharePoint web part. I am having some troubles setting the properties. Specifically, the ShowHeader property seems to be completely ignored, and it is set like so:

                    gv.ShowHeader = false;

This seems to work fine with the System.Web.UI.WebControls.DataGrid, which I had previously used. I'm also having this issue with many of the other properties, such as BorderColor, BorderWidth, etc. However, CellPadding and CellSpacing work just fine when set similarly:

                    gv.CellPadding = 2;
                    gv.CellSpacing = 2;

I don't understand where the problem is. Here is the DataGrid code I had been using, which worked fine:

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Specs");
                    DataSet flipped_ds = FlipDataSet(ds);
                    DataView dv = flipped_ds.Tables[0].DefaultView;
                    DataGrid outputGrid = new DataGrid();
                    outputGrid.DataSource = dv;
                    outputGrid.DataBind();
                    outputGrid.ShowHeader = false;
                    Controls.Add(outputGrid);

Here is the code that I have replaced this with for my GridView:

                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Bobst Specs");
                    DataSet flipped_ds = FlipDataSet(ds);
                    DataView dv = flipped_ds.Tables[0].DefaultView;
                    GridView gv = new GridView();
                    gv.DataSource = dv;
                    gv.DataBind();
                    gv.AutoGenerateColumns = true;
                    gv.CellPadding = 2;
                    gv.CellSpacing = 2;
                    gv.ShowHeader = false;
                    Controls.Add(gv);

Thanks for any help that I can get!

+1  A: 

have you tried setting the gv properties first and then binding?

Alan Stephens
Yes, I tried setting all of those before the binding statements, and the results were the same.
Geo Ego
After playing around for a while, I went back and tried setting the properties before binding again, and they worked! I don't know what I did wrong the first time, but it's working now. Thanks for your help!
Geo Ego
A: 

3 possible solutions --Try disable AutoGeneratedColumns gv.AutoGenerateColumns = false;

--Try add the control to the page first before setting any property

Or --Try a datatable instead and see if it works

                SqlDataAdapter da = new SqlDataAdapter(); 
                da.SelectCommand = cmd; 
                DataTable dt= new DataTable();
                dt.TableName = "Data";
                da.Fill(dt); 
                GridView gv = new GridView(); 
                gv.DataSource = dt; 
                gv.DataBind(); 
                gv.AutoGenerateColumns = true; 
                gv.CellPadding = 2; 
                gv.CellSpacing = 2; 
                gv.ShowHeader = false; 
                Controls.Add(gv);
madatanic