dynamic column/fields

Discussion in 'ASP .Net' started by =?Utf-8?B?RVI=?=, Jan 7, 2006.

  1. Hi,

    I would like to create a datagrid or free entry form with dynamic columns
    based on the user selection, do you have any ideas or samples?

    Any help would be appreciated!

    ER
    =?Utf-8?B?RVI=?=, Jan 7, 2006
    #1
    1. Advertising

  2. The datagrid server control provides a property named AutoGenerateColumns
    that would allow the grid to display any number of columns that are on the
    datatable.
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx

    You can make the datagrid editable by adding an EditCommandColumn
    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.editcommandcolumn.aspx

    Another alternative is to turn the AutoGenerateColumns to false and then
    compose the columns programmatically during databinding like this:
    private void dgItems_DataBinding(object sender, System.EventArgs e)
    {
    DataTable dt = (DataTable)dgItems.DataSource;
    for( int c=1; c<dt.Columns.Count; c++ )
    {
    BoundColumn bc = new BoundColumn();
    bc.DataField = dt.Columns[c].ColumnName;
    bc.HeaderText = dt.Columns[c].ColumnName;
    dgItems.Columns.Add( bc );
    }
    }

    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "ER" wrote:

    > Hi,
    >
    > I would like to create a datagrid or free entry form with dynamic columns
    > based on the user selection, do you have any ideas or samples?
    >
    > Any help would be appreciated!
    >
    > ER
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 7, 2006
    #2
    1. Advertising

  3. Thanks Phillip. For the second solution, would that affect the performance if
    the datagrid has significant data to display?

    Thanks,
    ER

    "Phillip Williams" wrote:

    > The datagrid server control provides a property named AutoGenerateColumns
    > that would allow the grid to display any number of columns that are on the
    > datatable.
    > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx
    >
    > You can make the datagrid editable by adding an EditCommandColumn
    > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.editcommandcolumn.aspx
    >
    > Another alternative is to turn the AutoGenerateColumns to false and then
    > compose the columns programmatically during databinding like this:
    > private void dgItems_DataBinding(object sender, System.EventArgs e)
    > {
    > DataTable dt = (DataTable)dgItems.DataSource;
    > for( int c=1; c<dt.Columns.Count; c++ )
    > {
    > BoundColumn bc = new BoundColumn();
    > bc.DataField = dt.Columns[c].ColumnName;
    > bc.HeaderText = dt.Columns[c].ColumnName;
    > dgItems.Columns.Add( bc );
    > }
    > }
    >
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
    > "ER" wrote:
    >
    > > Hi,
    > >
    > > I would like to create a datagrid or free entry form with dynamic columns
    > > based on the user selection, do you have any ideas or samples?
    > >
    > > Any help would be appreciated!
    > >
    > > ER
    =?Utf-8?B?RVI=?=, Jan 9, 2006
    #3
  4. I think that the performance would be the same.

    If the only requirement in building the datagrid were to display all columns
    without exception, such as the sample code below, then I prefer using the
    declarative syntax (the first solution) because it makes the code more
    readable and easily maintainable (one can change it without recompiling the
    application).

    If, on the other hand, one has to use different conditions for columns that
    may or may not be provided in the data or if one has to hide particular
    columns if they happen to exist in the data then the second approach would
    work better.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "ER" wrote:

    > Thanks Phillip. For the second solution, would that affect the performance if
    > the datagrid has significant data to display?
    >
    > Thanks,
    > ER
    >
    > "Phillip Williams" wrote:
    >
    > > The datagrid server control provides a property named AutoGenerateColumns
    > > that would allow the grid to display any number of columns that are on the
    > > datatable.
    > > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.autogeneratecolumns.aspx
    > >
    > > You can make the datagrid editable by adding an EditCommandColumn
    > > http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.editcommandcolumn.aspx
    > >
    > > Another alternative is to turn the AutoGenerateColumns to false and then
    > > compose the columns programmatically during databinding like this:
    > > private void dgItems_DataBinding(object sender, System.EventArgs e)
    > > {
    > > DataTable dt = (DataTable)dgItems.DataSource;
    > > for( int c=1; c<dt.Columns.Count; c++ )
    > > {
    > > BoundColumn bc = new BoundColumn();
    > > bc.DataField = dt.Columns[c].ColumnName;
    > > bc.HeaderText = dt.Columns[c].ColumnName;
    > > dgItems.Columns.Add( bc );
    > > }
    > > }
    > >
    > > --
    > > HTH,
    > > Phillip Williams
    > > http://www.societopia.net
    > > http://www.webswapp.com
    > >
    > >
    > > "ER" wrote:
    > >
    > > > Hi,
    > > >
    > > > I would like to create a datagrid or free entry form with dynamic columns
    > > > based on the user selection, do you have any ideas or samples?
    > > >
    > > > Any help would be appreciated!
    > > >
    > > > ER
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John E.
    Replies:
    3
    Views:
    4,458
    masoud bayan
    Mar 25, 2005
  2. call_me_anything
    Replies:
    4
    Views:
    459
    Pete Becker
    Sep 30, 2007
  3. johnsonlim026
    Replies:
    0
    Views:
    932
    johnsonlim026
    Feb 27, 2010
  4. bsm

    Dynamic GridView with Dynamic Column

    bsm, May 15, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    846
  5. John E.

    Move bound column to right of dynamic column in datagrid?

    John E., Mar 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    230
    Elton Wang
    Mar 28, 2005
Loading...

Share This Page