DataGrid Column sizing - Help

A

-Alex

I have snipped the following code from an example only to find that it
apparently doesn't work with regard to comlun sizing. I have been wrestling
with this to no avail.

The dataset is filled with the expected data but the size is wrong.
Whenever I run with the code below numeric fields have a size of 1, vchar
fields a size of 5 and date fields a size of 2.

If I comment the column.width statements they all seem to default to about a
size of 12.

Can anyone twist my head in the right direction?

Thanks,

-Alex



Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

Me.SqlDataAdapter1.Fill(Me.DataSet11)



'Step 1: Create a DataGridTableStyle &

' set mappingname to table.

Dim tableStyle As New DataGridTableStyle

tableStyle.MappingName = "Clinics"

'Step 2: Create DataGridColumnStyle for each col

' we want to see in the grid and in the

' order that we want to see them.



'Step 2: ClinincID

Dim column As New DataGridTextBoxColumn

column.MappingName = "ClinicID"

column.HeaderText = "Clinic ID"

column.Width = 12

tableStyle.GridColumnStyles.Add(column)

'Step 2: ClinicName

column = New DataGridTextBoxColumn

column.MappingName = "ClinicName"

column.HeaderText = "Clinic Name"

column.Width = 30

tableStyle.GridColumnStyles.Add(column)

'Step 2: Administrator

column = New DataGridTextBoxColumn

column.MappingName = "Administrator"

column.HeaderText = "Administrator"

column.Width = 30

tableStyle.GridColumnStyles.Add(column)

'Step 2: ActivityCount

column = New DataGridTextBoxColumn

column.MappingName = "ActivityCount"

column.HeaderText = "Activity Count"

column.Width = 12

tableStyle.GridColumnStyles.Add(column)

'Step 2: LastActivityDateTime

column = New DataGridTextBoxColumn

column.MappingName = "LastActivityDateTime"

column.HeaderText = "Last Activity"

column.Width = 15

tableStyle.GridColumnStyles.Add(column)

'Step 3: Add the tablestyle to the datagrid

Me.DataGrid1.TableStyles.Add(tableStyle)

End Sub
 
E

Eliyahu Goldin

Alex,

When it come to column width, the browser considers your instructions as
recommendations only. It will make columns as wide as needed to accommodate
the content. You can try to trick the browser by using non-breaking spaces,
some other tricks, bust the best advice is to relax and put up with the
browser's superiority.

Eliyahu
 
J

Jeffrey Tan[MSFT]

Hi Alex,

I have reviewed you post, and will reply you latter. Thanks for your
understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Alex,

Sorry for letting you wait for so long time.

I think the problem should be that you did not specify the correct
MappingName property for the DataGridTableStyle. You should set the
DataGridTableStyle.MappingName to the datatable's tablename.

The below code snippet works well:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
MakeParentTable()
Me.DataGrid1.DataSource = myDataSet.Tables(0)

Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Clinics"
Dim column As New DataGridTextBoxColumn
column.MappingName = "id"
column.HeaderText = "Clinic ID"
column.Width = 50
tableStyle.GridColumnStyles.Add(column)

column = New DataGridTextBoxColumn
column.MappingName = "Item"
column.HeaderText = "Clinic Item"
column.Width = 100
tableStyle.GridColumnStyles.Add(column)
tableStyle.MappingName = "MyTable"

Me.DataGrid1.TableStyles.Add(tableStyle)
End Sub

Dim myDataSet As DataSet
Private Sub MakeParentTable()
' Create a new DataTable.
Dim myDataTable As DataTable = New DataTable("MyTable")
' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

' Create new DataColumn, set DataType, ColumnName and add to
DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.Int32")
myDataColumn.ColumnName = "id"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)

' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("System.String")
myDataColumn.ColumnName = "Item"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)

' Instantiate the DataSet variable.
myDataSet = New DataSet
' Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable)

' Create three new DataRow objects and add them to the DataTable
Dim i As Integer
For i = 0 To 2
myDataRow = myDataTable.NewRow()
myDataRow("id") = i
myDataRow("Item") = "Item " + i.ToString()
myDataTable.Rows.Add(myDataRow)
Next i
End Sub

====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Alex,

Is your problem resolved? Do you still have any concern on this issue?
Please feel free to tell me. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top