Home All Groups Group Topic Archive Search About

setting datagrid column widths in code?

Author
27 Jan 2006 5:47 PM
Rich
Hello,

I have a one datagrid that will be based on different datatables.  One
datatable may have 7 columns, another 15...  With the tables that have more
columns, I have been manually dragging column widths to narrow them.  I want
to record these widths so I can reapply them when I bind the datagrid to the
table with 15 columns.  I have searched around for articles on this and saw
some that talked about datagrid.TableStyle.GridColumnStyle something,  but on
intellisense of my datagrid I got TableStyles and no GridColumnStyles.   I
would be very grateful if someone could set me straight on how I can retrieve
and set column widths for a datagrid from code.  Do I need to use a dataview
maybe to do this?

Thanks,
Rich

Author
27 Jan 2006 6:04 PM
I Don't Like Spam
Rich wrote:
Show quoteHide quote
> Hello,
>
> I have a one datagrid that will be based on different datatables.  One
> datatable may have 7 columns, another 15...  With the tables that have more
> columns, I have been manually dragging column widths to narrow them.  I want
> to record these widths so I can reapply them when I bind the datagrid to the
> table with 15 columns.  I have searched around for articles on this and saw
> some that talked about datagrid.TableStyle.GridColumnStyle something,  but on
> intellisense of my datagrid I got TableStyles and no GridColumnStyles.   I
> would be very grateful if someone could set me straight on how I can retrieve
> and set column widths for a datagrid from code.  Do I need to use a dataview
> maybe to do this?
>
> Thanks,
> Rich

I can tell you how to do the GridColumnStyle (it's
datagrid.TableStyle(0).GridColumnStyle) but there is a better way.

If you add one TableStyle for each of the datatables you are using the
grid will change automatically depending on which datatable is bound to
it.  The key that seems to get missed often on doing this is to make
sure the TableStyle.MappingName matches the name of the datatable.

Hope this helps.
Chris
Author
27 Jan 2006 6:38 PM
Rich
I am having a problem setting a table style for my datatable.  How do I do
that?

Dim dt As DataTable
dt = Dataset1.Tables("tbl1")
dt.???????
datagrid1.DataSource = dt


Show quoteHide quote
>
> I can tell you how to do the GridColumnStyle (it's
> datagrid.TableStyle(0).GridColumnStyle) but there is a better way.
>
> If you add one TableStyle for each of the datatables you are using the
> grid will change automatically depending on which datatable is bound to
> it.  The key that seems to get missed often on doing this is to make
> sure the TableStyle.MappingName matches the name of the datatable.
>
> Hope this helps.
> Chris
>
Author
27 Jan 2006 7:44 PM
Chris
Rich wrote:
Show quoteHide quote
> I am having a problem setting a table style for my datatable.  How do I do
> that?
>
> Dim dt As DataTable
> dt = Dataset1.Tables("tbl1")
> dt.???????
> datagrid1.DataSource = dt
>
>
>
>>I can tell you how to do the GridColumnStyle (it's
>>datagrid.TableStyle(0).GridColumnStyle) but there is a better way.
>>
>>If you add one TableStyle for each of the datatables you are using the
>>grid will change automatically depending on which datatable is bound to
>>it.  The key that seems to get missed often on doing this is to make
>>sure the TableStyle.MappingName matches the name of the datatable.
>>
>>Hope this helps.
>>Chris
>>


You set the tablestyle of the datagrid.
(doing this from memory, but you'll get the idea)

'Do this for each table
Dim DT as new DataGridTableStyle
DT.MappingName = "tbl1" '<--Important!

'Add one for each column you want displayed
Dim Column as new DataGridTextBoxColumn
Column.Width = 100
Column.DisplayName = ... 'This might be wrong property
Column.MappingName = ... 'This might be wrong property
DT.DataGridColumnStyle.Add(Column)
Column = new DataGridTextBoxColumn
Column.Width = 150
Column.DisplayName = ... 'This might be wrong property
Column.MappingName = ... 'This might be wrong property
DT.DataGridColumnStyle.Add(Column)

DataGrid1.TableStyles.Add(DT)

Dim dt As DataTable
datagrid1.DataSource = Dataset1.Tables("tbl1")

Hope this helps
Chris
Author
27 Jan 2006 8:19 PM
Rich
Thanks.  This should do the trick.  I might have to tweak it a little bit. 
But yes I get the idea now.

Rich

Show quoteHide quote
"Chris" wrote:

> Rich wrote:
> > I am having a problem setting a table style for my datatable.  How do I do
> > that?
> >
> > Dim dt As DataTable
> > dt = Dataset1.Tables("tbl1")
> > dt.???????
> > datagrid1.DataSource = dt
> >
> >
> >
> >>I can tell you how to do the GridColumnStyle (it's
> >>datagrid.TableStyle(0).GridColumnStyle) but there is a better way.
> >>
> >>If you add one TableStyle for each of the datatables you are using the
> >>grid will change automatically depending on which datatable is bound to
> >>it.  The key that seems to get missed often on doing this is to make
> >>sure the TableStyle.MappingName matches the name of the datatable.
> >>
> >>Hope this helps.
> >>Chris
> >>
>
>
> You set the tablestyle of the datagrid.
> (doing this from memory, but you'll get the idea)
>
> 'Do this for each table
> Dim DT as new DataGridTableStyle
> DT.MappingName = "tbl1" '<--Important!
>
> 'Add one for each column you want displayed
> Dim Column as new DataGridTextBoxColumn
> Column.Width = 100
> Column.DisplayName = ... 'This might be wrong property
> Column.MappingName = ... 'This might be wrong property
> DT.DataGridColumnStyle.Add(Column)
> Column = new DataGridTextBoxColumn
> Column.Width = 150
> Column.DisplayName = ... 'This might be wrong property
> Column.MappingName = ... 'This might be wrong property
> DT.DataGridColumnStyle.Add(Column)
>
> DataGrid1.TableStyles.Add(DT)
>
> Dim dt As DataTable
> datagrid1.DataSource = Dataset1.Tables("tbl1")
>
> Hope this helps
> Chris
>