将分隔的文本框行转换为数据网格视图的正确方法



请给我一些关于此错误的指导。我有一个TextBox输出,其中包含由;分隔的已经干净且格式化的文本,该文本对应于5列和无限行。但我不知道如何以正确的方式填充DataGridView。我收到这个错误

索引超出范围。必须是非负数并且小于集合的大小。参数名称:索引

示例文本框输出2行44

4c5e0c425cd;pppoe;4C:5E:0C:42:55:CD;192.168.29.71;2小时16分4C5E0C458887;pppoe;4C:5E:0C:45:88:87;192.168.29.110;

Private Sub QueryMkt_Click(sender As Object, e As EventArgs) Handles QueryMkt.Click
    DataGridView1.AutoGenerateColumns = False
    DataGridView1.Columns.Add("name", "name")
    DataGridView1.Columns.Add("service", "service")
    DataGridView1.Columns.Add("caller-id", "caller-id")
    DataGridView1.Columns.Add("address", "address")
    DataGridView1.Columns.Add("uptime", "uptime")
    For i As Integer = 0 To tbOutput.Lines.Length - 2
        Dim delimitedText As String = tbOutput.Lines(i)
        Dim holder As String() = Regex.Split(delimitedText, ";")
        'MessageBox.Show((i), (holder(0) + holder(4)))
        'DataGridView1.Item(0, i).Value = holder(0).ToString()
        DataGridView1.Rows(i).Cells("name").Value = holder(0).ToString()
        DataGridView1.Rows(i).Cells("service").Value = holder(1).ToString()
        DataGridView1.Rows(i).Cells("caller-id").Value = holder(2).ToString()
        DataGridView1.Rows(i).Cells("address").Value = holder(3).ToString()
        DataGridView1.Rows(i).Cells("uptime").Value = holder(4).ToString()
    Next
End Sub

您应该在应用程序的开头添加一次列,然后按照_latus的建议使用DataGridView.Rows.add()。你可以简单地通过"持有者"数组:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.AutoGenerateColumns = False
    DataGridView1.Columns.Add("name", "name")
    DataGridView1.Columns.Add("service", "service")
    DataGridView1.Columns.Add("caller-id", "caller-id")
    DataGridView1.Columns.Add("address", "address")
    DataGridView1.Columns.Add("uptime", "uptime")
End Sub
Private Sub QueryMkt_Click(sender As Object, e As EventArgs) Handles QueryMkt.Click
    For i As Integer = 0 To tbOutput.Lines.Length - 2
        Dim delimitedText As String = tbOutput.Lines(i)
        Dim holder As String() = Regex.Split(delimitedText, ";")
        DataGridView1.Rows.Add(holder)
    Next
End Sub

相关内容

  • 没有找到相关文章

最新更新