ASP.NET C# GridView.ShowHeader and other properties
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!
have you tried setting the gv properties first and then binding?
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);
精彩评论