setting datagrid column widths in code?

setting datagrid column widths in code?

Post by Umlja » Sun, 29 Jan 2006 02:47:07


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
 
 
 

setting datagrid column widths in code?

Post by I Don't Li » Sun, 29 Jan 2006 03:04:08


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

 
 
 

setting datagrid column widths in code?

Post by Umlja » Sun, 29 Jan 2006 03:38:28

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
 
 
 

setting datagrid column widths in code?

Post by Chri » Sun, 29 Jan 2006 04:44:28


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
 
 
 

setting datagrid column widths in code?

Post by Umlja » Sun, 29 Jan 2006 05:19:25

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

Rich