Grid's Column width

G

Guest

I am using .Net Framework 1.1 and ASP.Net & VB.Net.

I want to get width of each column after databind method. Please note I have
not assigned width design time, as I am not knowing how many columns will be
available. So at the runtime I am binding to datasource and later on I need
each column's width to set another control. Also there is a width property
available, but it gives always zero (0) in its value. Please help me to find
width in pixel.

Thanks in Advance.

- Kaushik
 
P

Peter Proost

Hi the datagrid has a PreferredColumnWidth property which gives you the
default column width, you can also use the DataGridTableStyle, for example:

Dim myCon As New SqlConnection("Data Source=server;Initial
Catalog=db;Integrated security=True")
Dim myDs As New DataSet
Dim myDa As New SqlDataAdapter("select * from ib015", myCon)

myCon.Open()
myDa.Fill(myDs, "ib015")
myCon.Close()

Dim tableStyle As DataGridTableStyle
tableStyle = New DataGridTableStyle
tableStyle.MappingName = "ib015"

' let the dataGrid use the tablestyle and bind it to our table
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(tableStyle)
DataGrid1.DataSource = myDs.Tables("ib015")

For i As Integer = 0 To myDs.Tables("ib015").Columns.Count - 1
MsgBox(DataGrid1.TableStyles("ib015").GridColumnStyles(i).Width)
Next


Hope this helps

Greetz, Peter
 

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