Click here to Skip to main content
12,757,549 members (38,126 online)
Rate this:
Please Sign up or sign in to vote.
Dear My Friends of Senior Programmer,

Please Help me, my Problem is cannot retrieve and load the color from my XML file into datagridview (EVENT: FORMTES3_LOAD). the application isn't error but the display of my Datagridview become irregular.

below is my FULL Source Code:

Note : the component is windows form, button, colordialog and datagridview

Imports System
Imports System.IO
Imports System.Xml

Public Class FormTes3

Dim Doc As XmlDocument = New XmlDocument

    Dim rowClicked As Integer
    Dim colorName As String

Private Sub CreateNewColorElement(ByVal rowID As Integer) ', ByVal columnIndex As Integer)

        'Create Row Node & ID Attribute
        Dim rowNode As XmlNode = Doc.CreateElement("Row")
        Dim idAttribute As XmlAttribute = Doc.CreateAttribute("rowID")
        idAttribute.Value = rowID.ToString

        'Column Node
        Dim columnNode As XmlNode = Doc.CreateElement("Profile")

        Dim backColor As XmlAttribute = Doc.CreateAttribute("BackColor")
        backColor.Value = colorName


    End Sub

Private Function GetHexColor(ByVal colorObj As System.Drawing.Color) As String
        Return "#" & Hex(colorObj.R) & Hex(colorObj.G) & Hex(colorObj.B)
    End Function

    Private Sub DataGridView1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseUp

        Dim result As DialogResult

        If e.Button = Windows.Forms.MouseButtons.Right Then

            rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex

    result = ColorDialog1.ShowDialog()

            If result = DialogResult.OK Then

                Dim rowIndex As Integer = rowClicked

   DataGridView1.Rows(rowClicked).DefaultCellStyle.BackColor = ColorDialog1.Color   'Running Well

                colorName = GetHexColor(ColorDialog1.Color)

 ElseIf result = DialogResult.Cancel Then


            End If

        End If

    End Sub

  Private Sub FormTes3_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        ' Populate the grid
        DataGridView1.Columns.Add("Column1", "Column 1")
        DataGridView1.Columns.Add("Column2", "Column 2")
        DataGridView1.Columns.Add("Column3", "Column 3")

        DataGridView1.Rows.Add("A", "B", "C")
        DataGridView1.Rows.Add("A", "B", "C")
        DataGridView1.Rows.Add("A", "B", "C")
        DataGridView1.Rows.Add("A", "B", "C")
        DataGridView1.Rows.Add("A", "B", "C")

        'In your constructor 

        If System.IO.File.Exists((Application.StartupPath + "\test.xml")) Then
            Dim row As Integer
            Dim column As Integer
            Dim backColor As String

            Doc = New XmlDocument
            Doc.Load((Application.StartupPath + "\test.xml"))

            For Each node As XmlNode In Doc.SelectNodes("//Row")
                row = Convert.ToInt32(node.Attributes("rowID").InnerText)

backColor = node.FirstChild.Attributes("BackColor").InnerText

                DataGridView1.Rows(row).DefaultCellStyle.BackColor = Color.FromName(backColor)


        End If

    End Sub

    Private Sub FormTes3_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'Doc.Save((Application.StartupPath + "\DataGrid.xml"))

        Dim saveFile As SaveFileDialog = New SaveFileDialog
        saveFile.InitialDirectory = Application.StartupPath
        saveFile.Filter = "XML files (*.xml)|*.xml"

        Dim dr As DialogResult = DialogResult.Yes

        If (MessageBox.Show("Do you want to save XML Style?", "Grid Style", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = dr) Then
            If (saveFile.ShowDialog = DialogResult.OK) Then
            End If

        End If

    End Sub

End Class

Thank you in advance for any help you can provide, my brothers :D


Agus Salim (Indonesia)


eMail: *******
Posted 27-Jul-12 0:37am
Updated 6-Aug-12 10:24am
Not a question; the issue is not explained.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your problem seems to be how you read you XML file. You could do something like this:

Dim name as string
 Public Sub Load(ByVal p_XMLfilename As String)
        XMLFilename = p_XMLfilename
        Dim x As New System.Xml.XmlDocument
        Dim x_nodes As System.Xml.XmlNodeList = x.SelectNodes("DataGridView")
        For Each node As System.Xml.XmlNode In x_nodes 
            For Each node2 As System.Xml.XmlNode In node
                Select Case node2.Name
                    Case Is = "FirstName" : name = node2.InnerText
                End Select
    End Sub

There also seems to be a problem regarding the storage of the values in you xml document.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100