Align or increase width for one column for DataGrid

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
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
VB.NET:
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.
 

Attachments

  • ScreenPic.zip
    40.8 KB · Views: 24
John, thank you for that help.

i check that example link you send. and i check couple of examples and in all those examples its taking values from database and put inside dataset and bind dataset to datagrid. but i'm taking values from an XML.

The code I’m using to display this table is
VB.NET:
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()

       Dim row As DataRow

        If File.Exists("C:\Program\Departments.xml") Then
            Dim myXmlDoc As XmlDocument
            Dim myNodelist As XmlNodeList
            Dim myNode As XmlNode
            Dim innerNode As XmlNode

            Dim strMonitor1 As String
            Dim strMonitor2 As String
            Dim strMonitor3 As String
            Dim strMonitor4 As String
            Dim strMonitor5 As String
            Dim strMonitor6 As String
            Dim strMonitor7 As String

            MonitorTable1.Rows.Clear()

            'Create the XML Document
            myXmlDoc = New XmlDocument
            'Load the Xml file
            myXmlDoc.Load("C:\Program\Departments.xml")
            'Get the list of name nodes
            myNodelist = myXmlDoc.GetElementsByTagName(strDep)

            For Each myNode In myNodelist
                Exit For
            Next

            If Not myNode Is Nothing Then
                innerNode = myNode.FirstChild
                row = MonitorTable1.NewRow()
                row("DepID") = innerNode.Attributes(0).Value
                row("Department") = innerNode.Attributes(1).Value
                strMonitor1 = innerNode.Attributes(2).Value
                strMonitor2 = innerNode.Attributes(3).Value
                strMonitor3 = innerNode.Attributes(4).Value
                strMonitor4 = innerNode.Attributes(5).Value
                strMonitor5 = innerNode.Attributes(6).Value
                strMonitor6 = innerNode.Attributes(7).Value
                strMonitor7 = innerNode.Attributes(8).Value

                If strMonitor1 = 1 Then
                    row(2) = True
                ElseIf strMonitor1 = 0 Then
                    row(2) = False
                End If

                MonitorTable1.Rows.Add(row)

                'Get Inner Node in XML
                Dim InnerSibling As XmlNode = innerNode.NextSibling
CheckNext1:
                While Not InnerSibling Is Nothing
                    row = MonitorTable1.NewRow()
                    row("DepID") = InnerSibling.Attributes(0).Value
                    row("Department") = InnerSibling.Attributes(1).Value

                    strMonitor1 = InnerSibling.Attributes(2).Value
                    strMonitor2 = InnerSibling.Attributes(3).Value
                    strMonitor3 = InnerSibling.Attributes(4).Value
                    strMonitor4 = InnerSibling.Attributes(5).Value
                    strMonitor5 = InnerSibling.Attributes(6).Value
                    strMonitor6 = InnerSibling.Attributes(7).Value
                    strMonitor7 = InnerSibling.Attributes(8).Value

                    If strMonitor1 = 1 Then
                        row(2) = True
                    ElseIf strMonitor1 = 0 Then
                        row(2) = False
                    End If

                    MonitorTable1.Rows.Add(row)
                    'Get Inner Sibling in XML
                    InnerSibling = InnerSibling.NextSibling
                    GoTo CheckNext1
                End While

                DataGrid1.DataSource = MonitorTable1
            End If
        End If
End Sub

i tried to create datagrid table style, but not able to bind the values i took from xml to datagrid. when a button is clicked, i need to display the xml values to datagrid. now i'm adding each values i'm taking from xml as rows to datatable and bind datatable to datagrid.

how come i bind xml values to datagrid. If you have any idea how to do this, please let me know. If you can provide an example then it will be great help for me.

Thanks in advance.
 
Back
Top