Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Closed Thread
Align or increase width for one column for DataGrid in Vb.net
Old 06-12-2008, 01:00 PM Align or increase width for one column for DataGrid in Vb.net
Experienced Talker

Posts: 35
Name: remya
Trades: 0
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 PreferredColumnWidth = 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.

I’m attaching two pictures with this. “CurrentScreen.bmp” will help you to know how the screen display when page loads. And “NewScreen.bmp” is the way I need to display the screen when page loads.

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.
Attached Files
File Type: zip ScreenPic.zip (40.8 KB, 2 views)
remya1000 is offline
View Public Profile
 
 
Register now for full access!
Old 06-17-2008, 06:27 PM Re: Align or increase width for one column for DataGrid in Vb.net
Experienced Talker

Posts: 35
Name: remya
Trades: 0
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.
remya1000 is offline
View Public Profile
 
Closed Thread     « Reply to Align or increase width for one column for DataGrid in Vb.net
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15886 seconds with 13 queries