By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,965 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,965 IT Pros & Developers. It's quick & easy.

dynamic column/fields

P: n/a
ER
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
Jan 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/lib...tecolumns.aspx

You can make the datagrid editable by adding an EditCommandColumn
http://msdn2.microsoft.com/en-us/lib...andcolumn.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

Jan 7 '06 #2

P: n/a
ER
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/lib...tecolumns.aspx

You can make the datagrid editable by adding an EditCommandColumn
http://msdn2.microsoft.com/en-us/lib...andcolumn.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

Jan 9 '06 #3

P: n/a
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/lib...tecolumns.aspx

You can make the datagrid editable by adding an EditCommandColumn
http://msdn2.microsoft.com/en-us/lib...andcolumn.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

Jan 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.