Reputation: 617
I am generating dynamic gridviews
and adding them to Panel
using Panel.Control.Add(<Gridviewname>)
. In my example, there are 3 Gridviews
out of which second one display content whereas rest of the two do not get populated. I had debugged the code and found that all Gridviews
are getting dataset
and successfully binding them but they are not showing it up. I added Label just to see that this might be the case that Panel
is not rendering multiple Gridviews
, so it might not render Label as well, but it did.
Following is the code:
private void DisplayData(DataSet _ds)
{
if ((_ds != null) && (_ds.Tables.Count > 0))
{
if (_ds.Tables[0].Rows.Count > 0)
{
GridView DyGV_Element = new GridView();
DyGV_Element.AutoGenerateColumns = false;
DyGV_Element.DataSource = null;
DyGV_Element.DataSource = _ds.Tables[0].DefaultView;
DyGV_Element.DataBind();
pnl_DisplayContent.Controls.Add(DyGV_Element);
}
if (_ds.Tables[1].Rows.Count > 0)
{
GridView DyGV_SubGroup = new GridView();
DyGV_SubGroup.DataSource = null;
DyGV_SubGroup.DataSource = _ds.Tables[1].DefaultView;
DyGV_SubGroup.DataBind();
pnl_DisplayContent.Controls.Add(DyGV_SubGroup);
}
if (_ds.Tables[3].Rows.Count > 0)
{
GridView DyGV_Comments = new GridView();
DyGV_Comments.AutoGenerateColumns = false;
DyGV_Comments.DataSource = null;
DyGV_Comments.DataSource = _ds.Tables[3].DefaultView;
DyGV_Comments.DataBind();
pnl_DisplayContent.Controls.Add(DyGV_Comments);
}
Label lblTest = new Label();
lblTest.Text = "Test content";
pnl_DisplayContent.Controls.Add(lblTest);
}
}
Following is the HTML output which shows that all gridviews are getting rendered but only second one shows data and label as well:
<div id="ctl00_ContentPlaceHolder_Front_pnl_DisplayContent">
<div>
<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
</table>
</div><div>
<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<tr>
<th scope="col">SubGroup</th>
</tr><tr>
<td>(T1) Technology Convergence and Alignment (test) (40%)</td>
</tr>
</table>
</div><div>
<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
</table>
</div><span>Test content</span>
Can any one tell me what is happening here? Why Gridviews do not show data where as they are populated?
EDIT: Solved by setting AutoGenerateColumns to true. Second grid has this true because it is not explicitly mentioned rest of the grids have this attribute set to false due to which it was not showing up the data. How stupid I am to post this question, it was simple and clear but did't strike me.
Thank you Daniel and Avneesh for your inputs, much appreciated.
Upvotes: 0
Views: 2362
Reputation: 654
The reason is that The AutoGenerateColumns = false on rest of the two grids. Tried the example :
void Page_PreRender(object sender, EventArgs e)
{
DataSet dataSet = Data.fetchDataSet();
GridView DyGV_Element = new GridView();
DyGV_Element.AutoGenerateColumns = false;
DyGV_Element.DataSource = dataSet.Tables[0].DefaultView;
DyGV_Element.DataBind();
GridView DyGV_SubGroup = new GridView();
DyGV_SubGroup.DataSource = dataSet.Tables[1].DefaultView;
DyGV_SubGroup.DataBind();
GridView DyGV_Comments = new GridView();
DyGV_Comments.AutoGenerateColumns = false;
DyGV_Comments.DataSource = dataSet.Tables[2].DefaultView;
DyGV_Comments.DataBind();
Panel1.Controls.Add(DyGV_Element);
Panel1.Controls.Add(DyGV_SubGroup);
Panel1.Controls.Add(DyGV_Comments);
}
Now I can only see the second Grid but if I remove or set the AutoGenerateColumns= true then I see all the grids. looks like some bug but in GridView source I can see the following:
if (AutoGenerateColumns == true) {
if (ColumnsGeneratorInternal is GridViewColumnsGenerator) {
((GridViewColumnsGenerator)ColumnsGeneratorInternal).DataItem = dataSource;
((GridViewColumnsGenerator)ColumnsGeneratorInternal).InDataBinding = useDataSource;
}
fieldsArray.AddRange(ColumnsGeneratorInternal.GenerateFields(this));
}
So you have to set the AutoGenerateColumns to bind the data.
Upvotes: 0