GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  ASP.NET 2.0
Go To First  |  Previous Question  |  Next Question 
 ASP.NET 2.0  |  Question 95 of 161    Print  
How to create dynamic Gridview?

  
Total Answers and Comments: 4 Last Update: April 19, 2007     Asked by: Dhanam 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
May 26, 2006 01:53:37   #1  
Chandra kumar        

RE: How to create dynamic Gridview?

Many times we have the requirement where we have to create columns dynamically.
This article describes you about the dynamic loading of data using the DataTable as the datasource.

Details of the Grid

Let s have a look at the code to understand better.

Create a gridview in the page

  1. Drag and drop the GridView on to the page

Or

  1. Manually type GridView definition in the page.

public partial class _Default : System.Web.UI.Page

{

#region constants

const string NAME NAME ;

const string ID ID ;

#endregion

protected void Page_Load(object sender EventArgs e)

{

loadDynamicGrid();

}

private void loadDynamicGrid()

{

#region Code for preparing the DataTable

//Create an instance of DataTable

DataTable dt new DataTable();

//Create an ID column for adding to the Datatable

DataColumn dcol new DataColumn(ID typeof(System.Int32));

dcol.AutoIncrement true;

dt.Columns.Add(dcol);

//Create an ID column for adding to the Datatable

dcol new DataColumn(NAME typeof(System.String));

dt.Columns.Add(dcol);

//Now add data for dynamic columns

//As the first column is auto-increment we do not have to add any thing.

//Let's add some data to the second column.

for (int nIndex 0; nIndex < 10; nIndex++)

{

//Create a new row

DataRow drow dt.NewRow();

//Initialize the row data.

drow[NAME] Row- + Convert.ToString((nIndex + 1));

//Add the row to the datatable.

dt.Rows.Add(drow);

}

#endregion

//Iterate through the columns of the datatable to set the data bound field dynamically.

foreach (DataColumn col in dt.Columns)

{

//Declare the bound field and allocate memory for the bound field.

BoundField bfield new BoundField();

//Initalize the DataField value.

bfield.DataField col.ColumnName;

//Initialize the HeaderText field value.

bfield.HeaderText col.ColumnName;

//Add the newly created bound field to the GridView.

GrdDynamic.Columns.Add(bfield);

}

//Initialize the DataSource

GrdDynamic.DataSource dt;

//Bind the datatable with the GridView.

GrdDynamic.DataBind();

}

}


 
Is this answer useful? Yes | No
August 23, 2006 06:42:40   #2  
Imran Parvez        

RE: How to create dynamic Gridview?
using JavaScript
 
Is this answer useful? Yes | No
December 09, 2006 01:27:30   #3  
Raj        

RE: How to create dynamic Gridview?

How about something like

GridView gv new GridView();

//set all gv's properties

Page.Controls.Add(gv);

Even better you can have runat server div and then add this control to the div. This will ensure that this grid appears at a fixed place.


 
Is this answer useful? Yes | No
April 19, 2007 11:22:26   #4  
TestPilot        

RE: How to create dynamic Gridview?
Only the first answer appropriate. The question is how to create a dynamically grid view which means populate the grid view dynamically.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape