Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid (.NET Compact Framework VS2005) 2

Status
Not open for further replies.

zarkon4

MIS
Joined
Dec 16, 2003
Messages
641
Location
US
Would anyone happen to know how I can autosize the cells of the datagrid with the size of my dataset table columns?

I know I would have to do this in code but not sure how to go about it.
 
This thread:

thread796-892736



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
zarkon4,

Here is some sample code I've used before to do exactly that:

Code:
'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

Then where ever you populate the grid call it like this:
Code:
SetColWidth()

Hope this helps or at least points you in the right direction.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"In the new millennium there will be two kinds of business; those on the net and those out of business"

~ Bill Gates ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top