.dat 文件导入datagridview问题?

jiangshufei 2011-12-17 02:09:58
jojoij.dat

文件。用记事本打开格式如下:

daad,00000,13556.6496,9144614.215,312.456
dfsaiwo,00000,6515364,134,2566156.1612,356.156

点击按钮控件将以上数据以逗号分隔导入到datagridview的5列中。

最号是vb.net代码。。谢谢。
...全文
115 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangshufei 2012-01-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhuhuailei 的回复:]
读取.dat文件和读取文本应该是一样的吧

Dim path As String = "c:jojoij.dat"
Try
If File.Exists(path) Then
Dim count as integer=0
Dim sr As StreamReader = New StreamReader(path)
Do While sr.Peek() > -1
……
[/Quote]


二楼的兄弟的代码很有用,有的地方是C写的.我改后的.
Dim path As String = "c:\jojoij.dat"
Try
If File.Exists(path) Then
Dim count As Integer = 0
Dim lie As Integer
Dim sr As StreamReader = New StreamReader(path)
Do While sr.Peek() > -1
Dim data() As String = sr.ReadToEnd().Split(",")
DataGridView1.Rows.Add() '先增加一行
 For lie = 0 To 4
Me.DataGridView1.Rows(count).Cells(lie).Value = data(lie)
Next
count = count + 1
Loop
End If
Catch
Show("不存在文件")
End Try
现在有两个问题.
1,导入的时候因为我最后一个数据后面没有"逗号"所以现在只能导入一行..
2,我想打开文件夹选择要导入的文件,因为文件不是固定的..
zhuhuailei 2011-12-20
  • 打赏
  • 举报
回复
读取.dat文件和读取文本应该是一样的吧

Dim path As String = "c:jojoij.dat"
Try
If File.Exists(path) Then
Dim count as integer=0
Dim sr As StreamReader = New StreamReader(path)
Do While sr.Peek() > -1
Dim data() as string = sr.split(',')
dataGridView1.Rows.Add(); //先增加一行
dataGridView1.Rows[count][0].Value = data(0) //给单元格赋值
dataGridView1.Rows[count][1].Value = data(1)
dataGridView1.Rows[count][2].Value = data(2)
。。。。。
count=count+1
Loop
End If
Catch
Throw("不存在文件")
End try

木有环境。不知道对不对。楼主自己试试吧。

苦苦的潜行者 2011-12-20
  • 打赏
  • 举报
回复
最好把txt中要导入dgv的数据整在一行,例如
c:\1.txt中的数据为
daad,00000,13556.6496,9144614.215,312.456,dfsaiwo,00000,6515364,134,2566156.1612,356.156



Dim sw as streamreader=new streamreader("c:\1.txt")
dim arr() as string=sw.readline().trim().split(",")
dgv.rows.add(arr) 'dgv为datagridview,当然你的dgv必须先有这么多列才行。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧