Load part of the XML file in GridView?

ManicCW

Well-known member
Joined
Jul 21, 2005
Messages
428
Location
Mostar
Programming Experience
Beginner
Hi I need to load this (these numbers are just example it will be some text):
VB.NET:
  <Dijelovi>
    <Zamjena>1;3;5;7</Zamjena>
    <Popravka>2;4;6</Popravka>
  </Dijelovi>
from XML file into GridView which has 2 Columns. I need it to look like this:

http://www.vbdotnetforums.com/attachment.php?attachmentid=666&stc=1&d=1150098278

This is the code where I need this:

VB.NET:
                'get data in controls
                For Each ctr As Control In doc.Controls
                    If TypeOf ctr Is TextBox Then
                        CType(ctr, TextBox).Text = xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText
                    ElseIf TypeOf ctr Is DateTimePicker Then
                        CType(ctr, DateTimePicker).Value = CDate(xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText)
                    ElseIf TypeOf ctr Is CheckBox Then
                        CType(ctr, CheckBox).Checked = CBool(xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText)
                    ElseIf TypeOf ctr Is ComboBox Then
                        CType(ctr, ComboBox).SelectedValue = xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText
                    ElseIf TypeOf ctr Is RadioButton Then
                        CType(ctr, RadioButton).Checked = CBool(xdoc.GetElementsByTagName(ctr.Name.Remove(0, 3)).Item(0).InnerText)
                    ElseIf TypeOf ctr Is DataGridView Then
                           [B]?????????[/B]
                    End If
                Next
Here is how I write it in XML:

VB.NET:
...
ElseIf TypeOf ctr Is DataGridView Then
                Dim row As DataGridViewRow
                Dim col As DataGridViewColumn
                xnode = xdoc.CreateElement(ctr.Name.Remove(0, 3))
                xdoc.DocumentElement.AppendChild(xnode)
                Dim catNode As XmlNode
                For Each col In CType(ctr, DataGridView).Columns
                    catNode = xdoc.CreateElement(col.Name)
                    For Each row In CType(ctr, DataGridView).Rows
                        If IsNothing(row.Cells(col.Index).Value) = False Then
                            catNode.InnerText &= row.Cells(col.Index).Value & ";"
                        End If
                    Next
                    catNode.InnerText = catNode.InnerText.Remove(catNode.InnerText.LastIndexOf(";"), 1)
                    xnode.AppendChild(catNode)
                Next
            End If
 

Attachments

  • gridview01.jpg
    gridview01.jpg
    11.1 KB · Views: 78
Get each child node of Dijelovi and split the text, add the number of rows needed to display the highest item count of the two, then set all the items. Here's how to add a specific number of rows and set the value of one specific cell:
VB.NET:
[SIZE=2]DataGridView1.Rows.Add(5)
DataGridView1.Item(0, 1).Value = [/SIZE][SIZE=2][COLOR=#800000]"2"
[/COLOR][/SIZE]
The columns and appearance is added in Designer or programmatically if you wish.
 
Well, you know how to get a string from a Xml node already, I told you how to add rows to DataGridView and how to set a value in a specific DataGridView cell, so the only piece I see you perhaps is missing is how to split a string: ("1;3;5;7")
VB.NET:
Dim nodevalue As String = "1;3;5;7"
Dim splitted() As String = nodevalue.Split(";")
 
Back
Top