无法将颜色加载/检索到Datagriview中(“使用XML在Datagridview中保存和检索颜色") [英] cannot load/retrieve color into Datagriview ("Save and Retrieve Color in a Datagridview using XML")

查看:94
本文介绍了无法将颜色加载/检索到Datagriview中(“使用XML在Datagridview中保存和检索颜色")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的高级程序员之友,

请帮助我,我的问题是无法从XML文件中检索颜色并将其加载到datagridview(事件:FORMTES3_LOAD).该应用程序不是错误,但我的Datagridview的显示变得不规则.

以下是我的完整源代码:

注意:该组件是Windows窗体,按钮,colordialog和datagridview


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
        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 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)
CreateNewColorElement(rowIndex)

 ElseIf result = DialogResult.Cancel Then

                Return

            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 
        Doc.AppendChild(Doc.CreateElement("DataGridView"))

        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)
                MsgBox(row)

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

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


  Next

        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
                Doc.Save(saveFile.FileName)
            End If

        End If

    End Sub



End Class




亲爱的兄弟,在此先感谢您提供的任何帮助:D


问候,

阿格斯·萨利姆(印度尼西亚)

网址:www.7global.asia

电子邮件:******* @ asia.com




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


Regards,

Agus Salim (Indonesia)

WEB: www.7global.asia

eMail: *******@asia.com

推荐答案

您的问题似乎是如何读取XML文件.您可以执行以下操作:

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
       x.Load(p_XMLfilename)

       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
           Next

       Next
   End Sub



关于在XML文档中存储值似乎也存在问题.



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


这篇关于无法将颜色加载/检索到Datagriview中(“使用XML在Datagridview中保存和检索颜色")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆