Setting the Column Width in DataGrids

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am binding a datatable to a datagrid. I can’t seem to find the right code
to control the column width. I searched the web and found this code:

Dim ColumnCount As Integer
ColumnCount = dtgAny.VisibleColumnCount()
Dim i As Short

Try
With dtgAny
Dim myTableStyle As DataGridTableStyle = New
DataGridTableStyle

.TableStyles.Add(myTableStyle)


For i = 0 To ColumnCount
dtgAny.TableStyles(0).GridColumnStyles(i).Width = 1000

Next
.TableStyles(0).DataGrid.Refresh()

.Refresh()


End With

When I run this, I get the message:

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

when i = 1

There is no effect on the grid.


Please help.
 
Hello Bob,

Looks like your GridColumnStyles collection has only had default initialization. Therefore the index of 0
is the only thing that won't cause an exc. Check the GridColumnStyles count in the debugger to ensure it has the elements you are trying to access.

Maybe list your code in its entirety to avoid any speculation on my behalf.

Best of luck!

Your C# ally ,
RBischoff


BA> Yes that's what I did, what I had was a misprint. it's failing when
BA> i = 1 anyway.
BA>
BA> "Phil Williams" wrote:
BA>
 
RBischoff said:
Hello Bob,

Looks like your GridColumnStyles collection has only had default initialization. Therefore the index of 0
is the only thing that won't cause an exc. Check the GridColumnStyles count in the debugger to ensure it has the elements you are trying to access.

Maybe list your code in its entirety to avoid any speculation on my behalf.

Best of luck!

Your C# ally ,
RBischoff

If you can be more specific and tell me the line(s) of code I need to
initialize the GridColumnSytles collections, that would be great.

Here is the entire routine as you requested:


Friend Sub SetColumnWidth(ByVal dtgAny As DataGrid)
Dim ColumnCount As Integer
ColumnCount = dtgAny.VisibleColumnCount()
Dim i As Short

Try
With dtgAny
Dim myTableStyle As DataGridTableStyle = New
DataGridTableStyle

.TableStyles.Add(myTableStyle)


For i = 0 To 0
dtgAny.TableStyles(0).GridColumnStyles(i).Width = 1000

Next
.TableStyles(0).DataGrid.Refresh()

.Refresh()


End With
Catch excAny As Exception
Call gcloGetMessage.DisplayError(excAny)
End Try



End Sub
 
Back
Top