no se puede cargar/recuperar color en Datagridview ("Guardar y recuperar color en Datagridview usando XML")

Mi problema es que no puedo recuperar y cargar el color de mi archivo XML en datagridview (en EVENTO: FORMTES3_LOAD). La aplicación no falla, pero la visualización de mi Datagridview se vuelve irregular/anormal.

Nota: el componente es un formulario de Windows (1 unidad - "FormTes3"), colordialog (1 unidad - "ColorDialog1") y datagridview (1 unidad - "Datagridview1")

A continuación se muestra mi código fuente completo:

Imports System
Imports System.IO
Imports System.Xml

Public Class FormTes3

Dim rowClicked As Integer
Dim colorName As String

Dim Doc As New XmlDocument


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

    Doc.AppendChild(Doc.CreateElement("DataGridView"))

    ' 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")


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

        docLoad.Load((Application.StartupPath + "\test.xml"))

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

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

            DataGridView1.Rows(row).DefaultCellStyle.BackColor = ColorTranslator.FromHtml(backColor)

          Next

    End If

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 CreateNewColorElement(ByVal rowID 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
    rowNode.Attributes.Append(idAttribute)

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

    'BackColor
    Dim backColor As XmlAttribute = Doc.CreateAttribute("BackColor")
    backColor.Value = colorName
    columnNode.Attributes.Append(backColor)

    rowNode.AppendChild(columnNode)
    Doc.DocumentElement.AppendChild(rowNode)

End Sub


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
        MsgBox("row Click: " & rowClicked)

        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)

            'If (Not (GlobalVariables.ColorStr) Is Nothing) Then
            CreateNewColorElement(rowIndex) 
            'End If

        ElseIf result = DialogResult.Cancel Then

            Return

        End If

    End If

End Sub


Private Sub FormTes3_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    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
            Doc.Save(saveFile.FileName)
        End If

    End If

End Sub

End Class

preguntado el 31 de julio de 12 a las 09:07

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.