Dynamically Creating a datagrid
-
Pls help me and suggest me some solutions , i want to create a datagrid in a class file not in aspx page i mean in using a class library when v select project type so the type i selected is class . now i want to genenrate a datagrid in this .cs file and show it no the page that will call this class say on the button click event .Pls help to create a datagrid in the .cs file so that i can call this class in the page and create the grid dynamically Patel Neelesh A
-
Pls help me and suggest me some solutions , i want to create a datagrid in a class file not in aspx page i mean in using a class library when v select project type so the type i selected is class . now i want to genenrate a datagrid in this .cs file and show it no the page that will call this class say on the button click event .Pls help to create a datagrid in the .cs file so that i can call this class in the page and create the grid dynamically Patel Neelesh A
You might want to try searching through the ASP.NET articles, because there is some really good info on datagrids. To get to your question, here is some code to help you get started in building a datagrid dynamically: Create the datagrid (I added my datagrid to a placeholder on the aspx page) protected DataGrid Grid = new DataGrid(); //Properties Grid.DataKeyField = "your field"; Grid.AutoGenerateColumns = false; Grid.CssClass = "Grid"; Grid.CellPadding = 5; Grid.CellSpacing = 0; Grid.AllowPaging = false; Grid.AllowSorting = false; Grid.Width = Unit.Percentage(100); Notice that I have the grid a cssclass. You can control borders and fonts using css, although it doesn't sound like you are too concerned with style at this point in your project. BoundColumn name = new BoundColumn(); name.HeaderText = "Header"; name.DataField = "db column"; Grid.Columns.Add(name) Next, go through the steps you would normally do for a datagrid... //Create the DataAdapter and DataSet SqlDataAdapter dataAdapter = new SqlDataAdapter(SqlString, ConnectionString); DataSet ds = new DataSet(); //Fill the dataset object dataAdapter.Fill(ds,"contractor"); //Bind the data to the DataGrid Grid.DataSource = ds; Grid.DataBind(); Next, add the grid to your placeholder. PlaceHolder1.Controls.Add(Grid); You're done. I'll let you implement that code into a class but the above should be just about everything you would need.
-
You might want to try searching through the ASP.NET articles, because there is some really good info on datagrids. To get to your question, here is some code to help you get started in building a datagrid dynamically: Create the datagrid (I added my datagrid to a placeholder on the aspx page) protected DataGrid Grid = new DataGrid(); //Properties Grid.DataKeyField = "your field"; Grid.AutoGenerateColumns = false; Grid.CssClass = "Grid"; Grid.CellPadding = 5; Grid.CellSpacing = 0; Grid.AllowPaging = false; Grid.AllowSorting = false; Grid.Width = Unit.Percentage(100); Notice that I have the grid a cssclass. You can control borders and fonts using css, although it doesn't sound like you are too concerned with style at this point in your project. BoundColumn name = new BoundColumn(); name.HeaderText = "Header"; name.DataField = "db column"; Grid.Columns.Add(name) Next, go through the steps you would normally do for a datagrid... //Create the DataAdapter and DataSet SqlDataAdapter dataAdapter = new SqlDataAdapter(SqlString, ConnectionString); DataSet ds = new DataSet(); //Fill the dataset object dataAdapter.Fill(ds,"contractor"); //Bind the data to the DataGrid Grid.DataSource = ds; Grid.DataBind(); Next, add the grid to your placeholder. PlaceHolder1.Controls.Add(Grid); You're done. I'll let you implement that code into a class but the above should be just about everything you would need.
Hey thanks alot for sending me the code but i cannot search on net since our organisation does not have that facility i have do it at my home posted the question .Can u pls tell me how to make datagrid in the webcontrols library since now our project requirement is so that v want make the datagrid in the webcontrols library. Patel Neelesh A