Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 12th, 2008, 10:57 AM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Align or increase width for 1 column in DataGrid

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.

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.
 
Old June 17th, 2008, 04:21 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Column Width Problem demivolt ASP.NET 1.0 and 1.1 Professional 1 July 22nd, 2005 09:05 AM
Setting Column width in Datagrid anita General .NET 2 April 13th, 2005 02:25 AM
DataGrid column width johanyu VS.NET 2002/2003 0 October 30th, 2004 05:29 AM
Datagrid Column Width resize problem Deadseven VS.NET 2002/2003 0 September 28th, 2004 09:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.