Unbound DataGrid (Cols count)- How To

  • Thread starter Thread starter Antonio Paglia
  • Start date Start date
A

Antonio Paglia

Does anyone know how to get the number of columns in unbound datagrid ?

TIA
Antonio
 
Antonio,
Does anyone know how to get the number of columns in unbound datagrid ?

By default a DataGrid doesn't have any columns. Unless you explicitly create
them using the TableStyles property (either at designtime or runtime), they
are created when you bind a data source to the grid, i.e. assign the
DataSource property.

So I think the answer to your question could be the following piece of code
(assuming you only have a single style defined):

int colCount = myGrid.TableStyles[0].GridColumnStyles.Count;

However, to make the above code more robust, you should check to see if
there are indeed any table styles present and that they have columns at all.
Alternatively, you could simply check the data source to see how many
columns it has.

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Thank you very much. This is very helpful for me.

Antonio


Jani Järvinen said:
Antonio,
Does anyone know how to get the number of columns in unbound datagrid ?

By default a DataGrid doesn't have any columns. Unless you explicitly
create them using the TableStyles property (either at designtime or
runtime), they are created when you bind a data source to the grid, i.e.
assign the DataSource property.

So I think the answer to your question could be the following piece of
code (assuming you only have a single style defined):

int colCount = myGrid.TableStyles[0].GridColumnStyles.Count;

However, to make the above code more robust, you should check to see if
there are indeed any table styles present and that they have columns at
all. Alternatively, you could simply check the data source to see how many
columns it has.

Hope this helps.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Back
Top