Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用vb.net读取文本文件数据并在dataGridView中显示_Vb.net_Datagridview - Fatal编程技术网

如何用vb.net读取文本文件数据并在dataGridView中显示

如何用vb.net读取文本文件数据并在dataGridView中显示,vb.net,datagridview,Vb.net,Datagridview,我对文本文件的读写是个新手。我需要读取文件并将每个单元格的数据存储在各自的数组中 我的文本文件具有以下字符:“|”表示列分隔符。第一列基于字符串,第二列和第三列基于整数。在dataGridView中有四列,第四列是第二列和第三列总数中的第二列百分比 Imports System.IO Public Class Form1 Dim teamName As String = "" Dim gamesWon As Integer = 0 Dim gamesLost As In

我对文本文件的读写是个新手。我需要读取文件并将每个单元格的数据存储在各自的数组中

我的文本文件具有以下字符:“|”表示列分隔符。第一列基于字符串,第二列和第三列基于整数。在dataGridView中有四列,第四列是第二列和第三列总数中的第二列百分比

Imports System.IO

Public Class Form1
    Dim teamName As String = ""
    Dim gamesWon As Integer = 0
    Dim gamesLost As Integer = 0
    Dim percentOfGamesWon As Double = (gamesWon + gamesLost) * gamesWon / 100%

    Sub reader()
        Dim textLine As String = ""
        Dim SplitLine() As String
        Using objReader As New StreamReader("C:\Users\WORK\Documents\text files\ALE.txt")
           Do While objReader.Peek() <> -1
              teamName = objReader.ReadLine()
              gamesWon = objReader.ReadLine()
              gamesLost = objReader.ReadLine()
              textLine = teamName & "|" & gamesWon & "|" & gamesLost & "|" & percentOfGamesWon
              SplitLine = Split(textLine, " ")
              Me.grdDisplay.Rows.Add(SplitLine)
          Loop
       End Using
    End Sub

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
       reader()
    End Sub
End Class
Imports System.IO
公开课表格1
Dim teamName As String=“”
Dim gamesWon为整数=0
Dim gamesLost作为整数=0
双重格默森的昏暗百分比=(格默森+格默森成本)*格默森/100%
副读取器()
Dim textLine As String=“”
将分割线()设置为字符串
使用objReader作为新的StreamReader(“C:\Users\WORK\Documents\text files\ALE.txt”)
Do While objReader.Peek()-1
teamName=objReader.ReadLine()
gamesWon=objReader.ReadLine()
gamesLost=objReader.ReadLine()
textLine=teamName&“|”&gamesWon&“|”&gamesLost&“|”&percentOfGamesWon
拆分行=拆分(文本行“”)
Me.grdDisplay.Rows.Add(分割线)
环
终端使用
端接头
私有子btnDisplay\u单击(发送者作为对象,e作为事件参数)处理btnDisplay。单击
读者()
端接头
末级
编辑:我更改了代码,因为我注意到我没有包括变量teamName、gamesWon、gamesLost和percentOfGamesWon


一、 但是,仍然有一个错误。我无法将objReader.Readline()与gamesWon和gamesLost一起使用。

您试图将整个数据行分配给各个变量。相反,您需要拆分由
ReadLine
返回的值,并将部分转换为适当的数据类型。添加
选项Strict On
也会有所帮助(在文件顶部或项目编译选项中)。您还可以最小化变量的范围-它们不需要在类级别声明

Sub reader()
    Using objReader As New StreamReader("C:\Users\WORK\Documents\text files\ALE.txt")
       Do While objReader.Peek() <> -1
          Dim line As String = objReader.ReadLine()
          Dim splitLine() As String = line.Split("|")
          Dim teamName As String = splitLine(0)
          Dim gamesWon As Integer = CInt(splitLine(1))
          Dim gamesLost As Integer = CInt(splitLine(2))
          Dim percentOfGamesWon As Double = gamesWon / (gamesWon + gamesLost) * 100
          Me.grdDisplay.Rows.Add(teamName, gamesWon, gamesLost, percentOfGamesWon)
      Loop
   End Using
End Sub
子读取器()
使用objReader作为新的StreamReader(“C:\Users\WORK\Documents\text files\ALE.txt”)
Do While objReader.Peek()-1
作为字符串的尺寸线=objReader.ReadLine()
尺寸拆分线()为String=line.Split(“|”)
Dim teamName作为字符串=拆分线(0)
Dim gamesWon作为整数=CInt(分割线(1))
Dim gamesLost As Integer=CInt(分割线(2))
双重格默森的昏暗百分比=格默森/(格默森+格默斯洛斯特)*100
Me.grdDisplay.Rows.Add(团队名称、gamesWon、gamesLost、gamesWon百分比)
环
终端使用
端接头

您试图将整个数据行分配给各个变量。相反,您需要拆分由
ReadLine
返回的值,并将部分转换为适当的数据类型。添加
选项Strict On
也会有所帮助(在文件顶部或项目编译选项中)。您还可以最小化变量的范围-它们不需要在类级别声明

Sub reader()
    Using objReader As New StreamReader("C:\Users\WORK\Documents\text files\ALE.txt")
       Do While objReader.Peek() <> -1
          Dim line As String = objReader.ReadLine()
          Dim splitLine() As String = line.Split("|")
          Dim teamName As String = splitLine(0)
          Dim gamesWon As Integer = CInt(splitLine(1))
          Dim gamesLost As Integer = CInt(splitLine(2))
          Dim percentOfGamesWon As Double = gamesWon / (gamesWon + gamesLost) * 100
          Me.grdDisplay.Rows.Add(teamName, gamesWon, gamesLost, percentOfGamesWon)
      Loop
   End Using
End Sub
子读取器()
使用objReader作为新的StreamReader(“C:\Users\WORK\Documents\text files\ALE.txt”)
Do While objReader.Peek()-1
作为字符串的尺寸线=objReader.ReadLine()
尺寸拆分线()为String=line.Split(“|”)
Dim teamName作为字符串=拆分线(0)
Dim gamesWon作为整数=CInt(分割线(1))
Dim gamesLost As Integer=CInt(分割线(2))
双重格默森的昏暗百分比=格默森/(格默森+格默斯洛斯特)*100
Me.grdDisplay.Rows.Add(团队名称、gamesWon、gamesLost、gamesWon百分比)
环
终端使用
端接头

OleDB可以将其读入数据表,这样您就可以将其绑定到DGV。请阅读,并采取…有更多的细节遗漏我显然不善于措辞我的问题,我知道。我没有使用OleDB。我应该将数据显示在DGV中并手动执行此操作。也许一些代码显示您尝试过的内容以及您如何使用问题描述来执行此操作会有所帮助。请阅读并同时获取添加的vb代码。很好,现在有问题了吗?它有用吗?有错误吗?如果是,是什么?问题是什么?OleDB可以将其读入数据表,这样您就可以将其绑定到DGV。请阅读,并采取…有更多的细节遗漏我显然不善于措辞我的问题,我知道。我没有使用OleDB。我应该将数据显示在DGV中并手动执行此操作。也许一些代码显示您尝试过的内容以及您如何使用问题描述来执行此操作会有所帮助。请阅读并同时获取添加的vb代码。很好,现在有问题了吗?它有用吗?有错误吗?如果是,是什么?问题是什么?它有效!谢谢如果我要以降序的方式按Gameswon的百分比对DGV进行排序,我将如何进行呢?我真的不知道如何实现按Gameswon的百分比排序我已经用
grdDisplay.sort(grdDisplay.Columns(3),ListSortDirection.descending)对它进行了排序。
它可以工作!谢谢如果我要以降序的方式按Gameswon的百分比对DGV进行排序,我该如何进行呢?我真的不知道如何实现按Gameswon的百分比排序我已经用
grdDisplay.sort(grdDisplay.Columns(3),ListSortDirection.descending)对其进行了排序。