Dynamically Adding Columns in GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dev147
    New Member
    • Jul 2007
    • 1

    #1

    Dynamically Adding Columns in GridView

    I need to dynamically create a GridView that contains a variable number of columns that must be bound to an underlying collection in an entity class.

    These columns represent days from a specified Start Date to a specified End Date. For eg. if the user selects 01/09 and 04/09, 4 columns need to be added to the GridView.

    My GridView is bound to an ObjectDataSourc e.
    The ObjectDataSourc e is configured to an entity class, say, TimeSheet.
    The TimeSheet entity class contains an int array, _days, which needs to be bound to the dynamically created columns in the GridView.

    This is the code for the function that creates the additional GridView columns:

    private void ModifyGrid()
    {
    int noColumnsRequir ed = DateDiffInDays( StartDate.DateV alue, EndDater.DateVa lue) + 1;
    DateTime currentDate = StartDatePicker .DateValue;

    for (int i = 0; (i < noColumnsRequir ed); i++)
    {

    // add a column to the gridview
    BoundField dayField = new BoundField();
    dayField.DataFi eld = "Day1"; // we need to bind to an underlying collection element!

    dayField.Header Text = "Day" + i;
    GridView1.Colum ns.Add(dayField );
    }
    }

    (Day1 is an int property in the entity class which I have used to test)

    My question is: How do I bind a GridView column to an underlying collection in an entity class?
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by Dev147
    I need to dynamically create a GridView that contains a variable number of columns that must be bound to an underlying collection in an entity class.

    These columns represent days from a specified Start Date to a specified End Date. For eg. if the user selects 01/09 and 04/09, 4 columns need to be added to the GridView.

    My GridView is bound to an ObjectDataSourc e.
    The ObjectDataSourc e is configured to an entity class, say, TimeSheet.
    The TimeSheet entity class contains an int array, _days, which needs to be bound to the dynamically created columns in the GridView.

    This is the code for the function that creates the additional GridView columns:

    private void ModifyGrid()
    {
    int noColumnsRequir ed = DateDiffInDays( StartDate.DateV alue, EndDater.DateVa lue) + 1;
    DateTime currentDate = StartDatePicker .DateValue;

    for (int i = 0; (i < noColumnsRequir ed); i++)
    {

    // add a column to the gridview
    BoundField dayField = new BoundField();
    dayField.DataFi eld = "Day1"; // we need to bind to an underlying collection element!

    dayField.Header Text = "Day" + i;
    GridView1.Colum ns.Add(dayField );
    }
    }

    (Day1 is an int property in the entity class which I have used to test)

    My question is: How do I bind a GridView column to an underlying collection in an entity class?
    What are the fields (column names) included in your datasource? Only these fields can be applied to your bound fields as a datafield.

    Nathan

    Comment

    Working...