Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
'This is the function to get the size of the field text
Public Function GetFieldSize(ByVal objDS As DataSet, ByVal TableName As String, ByVal ColumnName As String, ByVal objDataGrid As DataGrid, ByVal maxLen As Integer) As Integer
Dim maxLength As Integer = maxLen
Dim objGraphic As Graphics = objDataGrid.CreateGraphics
'Take width of one blank space and add to the new width of the column.
Dim offset As Integer = Convert.ToInt32(Math.Ceiling(objGraphic.MeasureString(" ", objDataGrid.Font).Width))
Dim i As Integer = 0
Dim intaux As Integer
Dim straux As String
Dim tot As Integer = objDS.Tables(TableName).Rows.Count
For i = 0 To (tot - 1)
straux = objDS.Tables(TableName).Rows(i).Item(ColumnName).ToString()
'Get the width of current field string according to the font.
intaux = Convert.ToInt32(Math.Ceiling(objGraphic.MeasureString(straux, objDataGrid.Font).Width))
If (intaux > maxLength) Then
maxLength = intaux
End If
Next
Return maxLength + offset
End Function
'This is the procedure to resize the column width based on the length of the text in it
Private Sub SetColWidth()
Dim strSQL As String
Dim objDA As OleDbDataAdapter
Dim objDS As New DataSet()
'Clear DataGrid table style.
grdProspectDetails.TableStyles.Clear()
'Initialize SQL string.
strSQL = "Your SQL Statement"
'Initialize the OleDbDataAdapter with the SQL and connection object.
objDA = New OleDbDataAdapter(strSQL, objConn)
'Use the OleDbDataAdapter to fill the DataSet with data.
objDA.Fill(objDS, "YourTableName")
'Binds the DataGrid to the DataSet.
grdProspectDetails.SetDataBinding(objDS, "YourName")
'Create new table style object.
Dim grdTableStyle As New DataGridTableStyle()
'Set the MappingName for the table style.
grdTableStyle.MappingName = "YourColumnStuff"
'Set the AlternatingBackColor property.
grdTableStyle.AlternatingBackColor = Color.WhiteSmoke
'CREATE FIRST COLUMN.
Dim grdTextColumn As New DataGridTextBoxColumn()
'Set the MappingName for the first column.
grdTextColumn.MappingName = "YourColumnName"
'Set header text for the first column.
grdTextColumn.HeaderText = "YourColumnHeader"
'Assign new width to DataGrid column.
grdTextColumn.Width = GetFieldSize(objDS, "YourColumnStuff", "YourColumnStuff", grdProspectDetails, grdTextColumn.Width)
'Call Add method of GridColumnStylesCollection object to add the column to the table style.
grdTableStyle.GridColumnStyles.Add(grdTextColumn)
'CREATE SECOND COLUMN.
'Get a new reference to the DataGridTextBoxColumn.
grdTextColumn = New DataGridTextBoxColumn()
'Set the MappingName for the second column.
grdTextColumn.MappingName = "YourColumnStuff"
'Set header text for the second column.
grdTextColumn.HeaderText = "YourColumnStuff"
'Assign new width to DataGrid column.
grdTextColumn.Width = GetFieldSize(objDS, "YourTableName", "YourColumnName", grdProspectDetails, grdTextColumn.Width)
'Call Add method of GridColumnStylesCollection object to add the column to the table style.
grdTableStyle.GridColumnStyles.Add(grdTextColumn)
'CREATE THIRD COLUMN.
'Get a new reference to the DataGridTextBoxColumn.
grdTextColumn = New DataGridTextBoxColumn()
'Set the MappingName for the third column.
grdTextColumn.MappingName = "YourColumnStuff"
'Set header text for the third column.
grdTextColumn.HeaderText = "YourColumnStuff"
'Assign new width to DataGrid column.
grdTextColumn.Width = GetFieldSize(objDS, "YourTableName", "YourColumnName", grdProspectDetails, grdTextColumn.Width)
'Call Add method of GridColumnStylesCollection object to add the column to the table style.
grdTableStyle.GridColumnStyles.Add(grdTextColumn)
'CREATE FOURTH COLUMN.
'Get a new reference to the DataGridTextBoxColumn.
grdTextColumn = New DataGridTextBoxColumn()
'Set the MappingName for the fourth column.
grdTextColumn.MappingName = "YourColumnStuff"
'Set header text for the fourth column.
grdTextColumn.HeaderText = "YourColumnStuff"
'Assign new width to DataGrid column.
grdTextColumn.Width = GetFieldSize(objDS, "YourTableName", "YourColumnName", grdProspectDetails, grdTextColumn.Width)
'Call Add method of GridColumnStylesCollection object to add the column to the table style.
grdTableStyle.GridColumnStyles.Add(grdTextColumn)
'Call Add method of GridTableStylesCollection object to add the table style to the DataGrid.
grdProspectDetails.TableStyles.Add(grdTableStyle)
End Sub
SetColWidth()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"In the new millennium there will be two kinds of business; those on the net and those out of business"
~ Bill Gates ~