all groups > asp.net datagrid control > february 2007 >
You're in the

asp.net datagrid control

group:

problem to creating dynamically gidview



problem to creating dynamically gidview yogesh joshi
2/7/2007 12:03:16 AM
asp.net datagrid control:

when ever i m creating my dynamic gridview not by (asp:gridview). On the
click of the link button its not displaying anything..and if i m
using(asp:gridview) then its working properly..

so plz help me out
my code is..

GridView grid = new GridView();
SqlDataAdapter adp = new SqlDataAdapter("select * from book
", ConfigurationManager.ConnectionStrings["cnn"].ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds);
//DataColumn dc = new DataColumn();
if (ds.Tables.Count > 0)
{
GridView2.Columns.Clear();
foreach (DataColumn dc in ds.Tables[0].Columns)
{
BoundField bField = new BoundField();
bField.DataField = dc.ColumnName;
bField.HeaderText = dc.ColumnName;
grid.Columns.Add(bField);
}

}
grid.DataSource = ds.Tables[0];
grid.DataBind();
grid.Visible = true;

Re: problem to creating dynamically gidview Teemu Keiski
2/15/2007 11:33:35 PM
Are you at any point adding the grid to Page's Controls collection? E.g say
you have a PAlceHolder on Page

....
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
....

After instantiating the grid, add it to the PalceHolder

GridView grid = new GridView();
PlaceHolder1.Controls.Add(grid);

Point is that a control must be in Controls hierarchy of the Page (and its
child controls)in order it to work in any way.


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net



[quoted text, click to view]
AddThis Social Bookmark Button