CoolInterview.com - World's Largest Collection of Interview Questions
 Interview Questions  
 Our Services  

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!


    Send your Resume to 6000 Companies


  • INTERVIEW QUESTIONS MICROSOFT ASP.NET 2.0 DETAILS
    Question :
    How to create dynamic Gridview?


    Category ASP.NET 2.0 Interview Questions
    Rating (4.0) By 3 users
    Added on 7/28/2006
    Views 1788
    Rate it!
    Answers:

    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,

    Drag and drop the GridView on to the page
    Or

    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();

    }

    }





    If you have the better answer, then send it to us. We will display your answer after the approval.
    Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    Web service support

    a) Data set
    b) dataReader
    c) both of above
    d) none of above


    View Answer
    In A Page I have gridview with Options of select and delete using hyperlink when i am selecting any one of then it has to open another page how can it

    View Answer
    What types of data validation events are commonly seen in the client-side form validation?

    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL ASP.NET 2.0 Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2009 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/8777/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | Joke of the Day

    0.47