Align or increase width for one column for DataGrid in Vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • remya1000
    New Member
    • Apr 2007
    • 115

    Align or increase width for one column for DataGrid in Vb.net

    I’m using Vb.net application program. I created a datagrid having text field and checkbox fields. Everything is displaying correctly. I have 9 Columns and each columns are equally separated. In DataGrid property, I made PreferredColumn Width = 85. So its equally aligned.

    I would like to give more width for the column named Department than rest of the columns. Because when its equally aligned, I can see the Departments displayed.

    Department is the second column. that having long names under that. so when page loads, its only displaying half of that. so if i increase that column width i can see that. but i don't need to increase the other columns width. make rest of the columns width smaller and increase the second columns width.

    The code I’m using to display this table is
    Code:
    Private Sub MainForm_Load(ByVal …………… ) Handles Me.Load
            InitializeDataGrid()
            getDepartments()
    End Sub
    
    Private Sub InitializeDataGrid()
      Dim column1 As DataColumn
            MonitorTable1 = New DataTable("MonitorTable")
    
            ' Create "Dep ID" column
            column1 = New DataColumn("DepID", GetType(Integer))
            MonitorTable1.Columns.Add(column1)
    
            ' Create "Dep Name" column
            column1 = New DataColumn("Department", GetType(String))
            MonitorTable1.Columns.Add(column1)
    
            ' Create a column for each monitor
            For i As Integer = 1 To 7
                column1 = New DataColumn("Monitor " & i.ToString(), GetType(Boolean))
                
                column1.AllowDBNull = False
                column1.DefaultValue = False
    
                MonitorTable1.Columns.Add(column1)
            Next
            DataGrid1.DataSource = MonitorTable1
    End Sub
    
    Sub getDepartments()
    
            ' This is where you might ask the database how many departments there are
            MonitorTable1.Rows.Clear()
            myConnection.Open()
            Dim strSQL As String = "Select DepID, DepName from Dep order by DepName"
            Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
            Dim myReader As OleDbDataReader = myCommand.ExecuteReader
            While myReader.Read
                Dim row As DataRow = MonitorTable1.NewRow()
                row("DepID") = myReader(0)
                row("Department") = myReader(1)
                MonitorTable1.Rows.Add(row)
            End While
            myReader.Close()
            myConnection.Close()
            DataGrid1.DataSource = MonitorTable1
    
    End Sub
    If you have any idea how to align datagrid, please let me know. If you can provide an example then it will be great help for me.

    Thanks in advance.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Isn't there an autowidth property that allows you to make the columns as wide as the header text, or as wide as the column data or neither or both?

    Comment

    • remya1000
      New Member
      • Apr 2007
      • 115

      #3
      To made auto width, inside DataGrid properties PreferredColumn Width property set to 85. so all the column width are equally aligned but not wide as column data.

      but i want the second column more wide than other columns. because only the second column having long data.

      if you have any idea how to do this, please help me. if you can provide an example then it will be great.

      Thanks in advance.

      Comment

      • remya1000
        New Member
        • Apr 2007
        • 115

        #4
        This code will help to align the datagrid according to the contents (data) length in each columns.

        Code:
        SizeColumnsToContent(Me.DataGrid1, -1)
        
        Public Sub SizeColumnsToContent(ByVal dataGrid As DataGrid, ByVal nRowsToScan As Integer)
        
                ' Create graphics object for measuring widths.
                Dim Graphics As Graphics = DataGrid1.CreateGraphics
        
                ' Define new table style.
                Dim tableStyle As DataGridTableStyle = New DataGridTableStyle
        
                Try
                    Dim dataTable As DataTable = dataGrid.DataSource
        
                    If (-1 = nRowsToScan) Then
                        nRowsToScan = dataTable.Rows.Count
                    Else
                        ' Can only scan rows if they exist.
                        nRowsToScan = System.Math.Min(nRowsToScan, dataTable.Rows.Count)
                    End If
        
                    ' Clear any existing table styles.
                    dataGrid.TableStyles.Clear()
        
                    ' Use mapping name that is defined in the data source.
                    tableStyle.MappingName = dataTable.TableName
        
                    ' Now create the column styles within the table style.
                    Dim textColumnStyle As DataGridTextBoxColumn
                    Dim iWidth As Integer
                    Dim iCurrCol As Integer
                    For iCurrCol = 0 To dataTable.Columns.Count - 8
                        Dim dataColumn As DataColumn = dataTable.Columns(iCurrCol)
        
                        textColumnStyle = New DataGridTextBoxColumn
                        textColumnStyle.TextBox.Enabled = True
                        textColumnStyle.HeaderText = dataColumn.ColumnName
                        textColumnStyle.MappingName = dataColumn.ColumnName
        
                        ' Set width to header text width.
                        iWidth = (Graphics.MeasureString(textColumnStyle.HeaderText, dataGrid.Font).Width)
        
                        ' Change width, if data width is wider than header text width.
                        ' Check the width of the data in the first X rows.
                        Dim DataRow As DataRow
                        Dim iRow As Integer
                        For iRow = 0 To nRowsToScan - 1
                            DataRow = dataTable.Rows(iRow)
                            Dim iColWidth As Integer
                            iColWidth = (Graphics.MeasureString(DataRow.ItemArray(iCurrCol).ToString(), dataGrid.Font).Width)
                            iWidth = System.Math.Max(iWidth, iColWidth)
                        Next
        
                        textColumnStyle.Width = iWidth + 4
        
                        ' Add the new column style to the table style.
                        tableStyle.GridColumnStyles.Add(textColumnStyle)
                    Next
        
                    ' Now create the column styles within the table style.
                    Dim boolColumnStyle As DataGridBoolColumn
                    For iCurrCol = 2 To dataTable.Columns.Count - 1
                        Dim dataColumn As DataColumn = dataTable.Columns(iCurrCol)
        
                        boolColumnStyle = New DataGridBoolColumn
                        boolColumnStyle.AllowNull = False
                        boolColumnStyle.HeaderText = dataColumn.ColumnName
                        boolColumnStyle.MappingName = dataColumn.ColumnName
        
                        ' Set width to header text width.
                        iWidth = (Graphics.MeasureString(boolColumnStyle.HeaderText, dataGrid.Font).Width)
        
                        ' Change width, if data width is wider than header text width.
                        ' Check the width of the data in the first X rows.
                        Dim DataRow As DataRow
                        Dim iRow As Integer
                        For iRow = 0 To nRowsToScan - 1
                            DataRow = dataTable.Rows(iRow)
                            Dim iColWidth As Integer
                            iColWidth = (Graphics.MeasureString(DataRow.ItemArray(iCurrCol).ToString(), dataGrid.Font).Width)
                            iWidth = System.Math.Max(iWidth, iColWidth)
                        Next
        
                        boolColumnStyle.Width = iWidth + 4
        
                        ' Add the new column style to the table style.
                        tableStyle.GridColumnStyles.Add(boolColumnStyle)
                    Next
                    ' Add the new table style to the data grid.
                    dataGrid.TableStyles.Add(tableStyle)
                Catch e As Exception
                Finally
                    Graphics.Dispose()
                End Try
        
            End Sub
        hope this will help someone.

        Comment

        Working...