Datagridview没有在vb中显示正确的信息

时间:2012-10-18 03:17:08

标签: vb.net datagridview

我有一个维护电话簿的程序。该程序一直有效,直到它显示目录。它应该显示从列表框中选择的目录,然后在datagridview中显示信息。这是我的代码:

   Option Strict On

Imports System.IO

公共类frmTelephoneDirectories     结构人         Dim name As String         Dim number As String     结束结构

Structure Directory
    Dim File As String
    Dim records() As people
    Dim recordCount As Integer
End Structure

Dim directories() As Directory
Dim directoryCount As Integer = 0
Dim temp As String
Dim tempreader As IO.StreamReader
Dim tempBuffer() As String
Dim position As Integer

Private Sub frmTelephoneDirectories_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim sw As IO.StreamWriter = IO.File.CreateText("Directories.txt")
    sw.WriteLine("Friends.txt")
    sw.WriteLine("Merchants.txt")
    sw.WriteLine("Restaurants.txt")
    sw.WriteLine("Theaters.txt")
    sw.Close()
    Dim sr As IO.StreamReader = IO.File.OpenText("Directories.txt")
    lstTextFiles.Items.Clear()
    Do While sr.Peek <> -1
        temp = sr.ReadLine()
        lstTextFiles.Items.Add(temp)
        ReDim Preserve directories(lstTextFiles.Items.Count())
        directories(lstTextFiles.Items.Count() - 1).File = temp
        tempreader = IO.File.OpenText(temp)
        While (tempreader.Peek() <> -1)
            temp = tempreader.ReadLine()
            tempBuffer = temp.Split(","c)
            position = lstTextFiles.Items.Count() - 1
            directories(position).recordCount += 1
            ReDim Preserve directories(position).records(directories(position).recordCount)
            directories(position).records(directories(position).recordCount - 1).name = tempBuffer(0)
            directories(position).records(directories(position).recordCount - 1).number = tempBuffer(1)
        End While
        tempreader.Close()
    Loop
    sr.Close()
End Sub

Private Sub btnCreateNew_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateNew.Click

    Dim directoryName As String = InputBox("Enter a directory name")
    Dim writer As IO.StreamWriter
    directoryCount += 1

    ReDim Preserve directories(directoryCount)
    directories(directoryCount - 1).File = directoryName
    writer = IO.File.AppendText("directories.txt")
    writer.WriteLine(directoryName)
    lstTextFiles.Items.Add(directoryName)

    If directoryName = "" Then
        MessageBox.Show("Please enter in the directory name!")
    End If
End Sub

Private Sub btnAddListing_Click(sender As System.Object, e As System.EventArgs) Handles btnAddListing.Click

    Dim name As String = ""
    Dim number As String = ""

    name = txtName.Text
    number = txtPhone.Text

    directories(lstTextFiles.SelectedIndex).recordCount += 1
    ReDim Preserve directories(lstTextFiles.SelectedIndex).records(directories(lstTextFiles.SelectedIndex).recordCount)
    directories(lstTextFiles.SelectedIndex).records(directories(lstTextFiles.SelectedIndex).recordCount - 1).name = name
    directories(lstTextFiles.SelectedIndex).records(directories(lstTextFiles.SelectedIndex).recordCount - 1).number = number

End Sub

Private Sub lstFiles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTextFiles.SelectedIndexChanged

    txtDirectory.Text = CStr(lstTextFiles.SelectedItem)

End Sub

Private Sub btnRemoveListing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveListing.Click

    Dim tempData() As people
    Dim name As String = txtName.Text
    Dim position As Integer = 0
    Dim counter As Integer = 0

    For i As Integer = 0 To directories(lstTextFiles.SelectedIndex).records.GetUpperBound(0) - 1
        If (directories(lstTextFiles.SelectedIndex).records(i).name <> name) Then
            counter += 1
            ReDim Preserve tempData(counter)
            tempData(counter - 1).name = directories(lstTextFiles.SelectedIndex).records(i).name
            tempData(counter - 1).number = directories(lstTextFiles.SelectedIndex).records(i).number
        End If
    Next
    directories(lstTextFiles.SelectedIndex).records = tempData
End Sub


Private Sub btnDisplayListings_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplayListings.Click

    If (directories(lstTextFiles.SelectedIndex).records Is Nothing) Then
        DgvDirectory.DataSource = Nothing
        Return
    End If

    Dim directoryS = From data In directories(lstTextFiles.SelectedIndex).records
                     Select data.name, data.number
                     Order By name

    DgvDirectory.DataSource = IO.File.ReadAllLines("directories.txt")
End Sub

结束班

0 个答案:

没有答案