如何在TabControl(vb.net)中遍历网格的行?

时间:2012-11-24 11:08:41

标签: vb.net datagridview tabcontrol

我有一个标签控件,其中包含多个标签页,每个标签页都添加了一个网格。当我单击“另存为”按钮时,我希望程序遍历每个选项卡上的网格,并以类似XML的格式保存数据。

该文件应如下所示:

<GridTabPage1>
<Row1> A, B, C ... </Row1>
</GridTabPage1>

我目前正在尝试但它无法正常工作:

For Each row As DataGridView In TabControl.SelectedTab.Controls(Grid.Rows)
...
Next

有人可以指出我正确的方向吗?

我希望我对问题的解释不会令人困惑。如果您需要澄清,请告诉我。

谢谢你们

2 个答案:

答案 0 :(得分:2)

Using xw As New XmlTextWriter("C:\test.xml", System.Text.Encoding.Default)
        xw.Formatting = Formatting.Indented
        xw.WriteStartDocument()
        xw.WriteStartElement("grids")
        For Each tp As TabPage In Me.TabControl1.Controls.OfType(Of TabPage)()
            For Each dgv As DataGridView In tp.Controls.OfType(Of DataGridView)()
                xw.WriteStartElement("DataGrid")
                For Each row As DataGridViewRow In dgv.Rows
                    xw.WriteStartElement("row")
                    For Each cell As DataGridViewCell In row.Cells
                        xw.WriteElementString("cell", cell.Value)
                    Next
                    xw.WriteEndElement()
                Next
                xw.WriteEndElement()
            Next
        Next
        xw.WriteEndElement()
        xw.WriteEndDocument()

    End Using

答案 1 :(得分:1)

尝试这样的事情:

For Each tp As TabPage In TabControl1.TabPages
  For Each dgv As DataGridView In tp.Controls.OfType(Of DataGridView)()

  Next
Next

它循环遍历所有TabPages,对于每个TabPage,它将找到该页面上的任何DataGridView控件。