Changing the Column Width and Title dynamically

G

Guest

Hi,

I have a form that has a Datagrid. The datagrid is use to display data from
different table selected by user onme at a time.

I can programatically customized the Column's Title of the DataGrid by
specifying alias in the SQL statement.
Ex : SELECT strWhseID as [Warehouse ID], strType As [Warehouse Type] from
tblWarehouse

But, I have difficulty in adjusting their width programatically.
I have try creating a DataGridTextBoxColumn Collection and adding it into
the datagrid tablestyle collection and subsequently adding it into the
datagrid tablestyle collection but it just doesn't work.


Anyone help?
 
G

Guest

I use this function for each datagridcolumn of my datagrid:

Public Function GetWidthColonna(ByVal C As DataGridColumnStyle) As Integer
Dim I As Integer, N As Integer
Dim S As SizeF, W As Single, R As Integer
Dim G As Graphics = Graphics.FromHwnd(Handle)
Dim F As New StringFormat(StringFormat.GenericTypographic)
If TypeName(C) = "DataGridBoolColumn" Then N = 0 Else N =
VisibleRowCount
For R = -1 To N - 1
If R = -1 Then
C.HeaderText = C.HeaderText.ToUpper
S = G.MeasureString(C.HeaderText, Font, 500, F)
Else
I = C.DataGridTableStyle.GridColumnStyles.IndexOf(C)
S = G.MeasureString(Me(R, I).ToString, Font, 500, F)
End If
If (S.Width > W) Then W = S.Width
Next R : G.Dispose() : F.Dispose() : Return CType(W, Integer) + 10
End Function

I hope it can be useful for You...
 

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