setting datagrid column widths in code?

G

Guest

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
 
G

Guest

Rich said:
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
 
G

Guest

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
 
C

Chris

Rich said:
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


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
 
G

Guest

Thanks. This should do the trick. I might have to tweak it a little bit.
But yes I get the idea now.

Rich
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top