(救命呀)C#如何读取文本文件并显示在DataGridView中

woheni2008523 2009-08-26 12:06:03
我想实现这样一个功能,界面上有两个DataGridView控件,点击其中一个DataGridView中的某记录,即可从数据库中读取相应的文本文件中的内容(如下所示),并显示在另一个DataGridView控件中,请大家帮忙想一些具体的代码应咋写,我是个新手,感觉这不是个很难的问题,可是就是搞不定,快愁死了!

文本文件的内容如下:

EportSoft Tube ... L α β R (这一行不显示)
22.000
13.809 0.000 55.585 44.000
143.128 0.000 0.000 0.000
#eof(这一行也不现实)

或者

EportSoft Tube ... (这一行不显示)
75
0.000 0.000 90.000 75.000
108.048 90.000 28.809 150.000
48.400 0.000 0.000 0.000
#eof(这一行不显示)


...全文
1036 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
liaoxionghua 2010-10-26
  • 打赏
  • 举报
回复
谢谢楼主的答案,我找了很久!!
jetwangstar 2010-05-22
  • 打赏
  • 举报
回复
'读取文本文件
Private Sub Read_File(ByVal filename As String)
Dim dTable As New DataTable
Dim Reader As New StreamReader(filename, System.Text.Encoding.Default) '参数分别为带路径的文件名和字符编码
Dim sLine As String = Reader.ReadLine()
Dim sVals(), sVal As String
Dim i As Integer
i = 0
While Not (sLine Is Nothing)
'文件中使用tab键作为分隔符
sVals = Split(sLine, ControlChars.Tab)
If i = 0 Then
For Each sVal In sVals
dTable.Columns.Add(sVal)
Next
Else
dTable.Rows.Add(sVals)
End If
sLine = Reader.ReadLine()
i = i + 1
End While
DataGridView1.DataSource = dTable
AsYouLikeIt 2010-03-29
  • 打赏
  • 举报
回复
day day up
woheni2008523 2009-08-31
  • 打赏
  • 举报
回复
自己基本搞定,结贴,谢谢大家的热心帮助!
woheni2008523 2009-08-28
  • 打赏
  • 举报
回复
谢谢大家的思路提示,思路我是明白了,可是代码怎么来写呢?字符串处理的代码有怎么写呢?我怎么写才能不显示文本中的第一行和最后一行,将文本中的中间不确定的几行读取显示到datagridview中呢?
youdandan 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sunshine_anycall 的回复:]
可以参考下这个,然后差不多就是字符串处理的问题了
C# codeusing System;using System.IO;class Test
{publicstaticvoid Main()
{string path=@"c:\temp\MyTest.txt";// This text is added only once to the file.if (!File.Exists(path))
{// Create a file to write to.string[] createText= {"Hello","And","Welcome" };
File.WriteAllLines(path, createText);
}// This text is always added, making the file longer over time// if it is not deleted.string appendText="This is extra text"+ Environment.NewLine;
File.AppendAllText(path, appendText);// Open the file to read from.string[] readText= File.ReadAllLines(path);foreach (string sin readText)
{
Console.WriteLine(s);
}
}
}
[/Quote]
jiu採用字符流處理
robin521 2009-08-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sunshine_anycall 的回复:]
可以参考下这个,然后差不多就是字符串处理的问题了
C# codeusing System;using System.IO;class Test
{publicstaticvoid Main()
{string path=@"c:\temp\MyTest.txt";// This text is added only once to the file.if (!File.Exists(path))
{// Create a file to write to.string[] createText= {"Hello","And","Welcome" };
File.WriteAllLines(path, createText);
}// This text is always added, making the file longer over time// if it is not deleted.string appendText="This is extra text"+ Environment.NewLine;
File.AppendAllText(path, appendText);// Open the file to read from.string[] readText= File.ReadAllLines(path);foreach (string sin readText)
{
Console.WriteLine(s);
}
}
}
[/Quote]

嗯,我试过了,在记事本中写入文字,那么我帮楼主提一个思路,既然二楼已经给出了读和写文本文件中的方法,楼主就可以就只用读取字段,然后添加到DataGridView里面,添加到DataSet["Tabel"]里面就行了.
whowhen21 2009-08-27
  • 打赏
  • 举报
回复
顶。。。

****************************************************************
看帖一定要回的,分也一定要接的。^_^
****************************************************************
robin521 2009-08-27
  • 打赏
  • 举报
回复
我先帮楼主看看二楼的代码是干嘛的.
24K純帥 2009-08-27
  • 打赏
  • 举报
回复
顶,顺便学习
woheni2008523 2009-08-27
  • 打赏
  • 举报
回复
顶呀!
woheni2008523 2009-08-27
  • 打赏
  • 举报
回复
我尝试了以下好几种方法,但是结果不是出错就是现实不正确,大家帮忙看看错误出在哪里?谢谢了!
第一种如下:
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fs);
string columnInfo = sr.ReadLine();
char[] seperator = new char[] { ' ' };
string[] columns = columnInfo.Trim().Split) //错误在这里 未将对象引用设置到对象的实例
(seperator,StringSplitOptions.RemoveEmptyEntries ); this.dataGridView_导管实样坐标信息.Rows.Add(columns);
sr.Close(); 但是这个显示出错了。

第二种方法:
using (TextFieldParser myReader = new TextFieldParser(filePath))
{
string[] currentRow;
this.dataGridView_导管实样坐标信息.Rows.Clear();
myReader.TextFieldType = FieldType.Delimited;
myReader.Delimiters = new String[] { " " };//出错,分隔符不能为空,可是我改为“,”后显示出来的是将一行的数据全部显示在DataGridview的一个单元格中
while (!myReader.EndOfData)
{
try
{

currentRow = myReader.ReadFields(); this.dataGridView_导管实样坐标信息.Rows.Add(str);

}


catch (MalformedLineException ex)
{
MessageBox.Show("行 " + ex.Message + " 是无效的。略过。");
}
}
}

出错:分隔符不能为空,可是将字段两头不需要的字符也显示了!



大家快帮忙想想办法!
guyehanxinlei 2009-08-26
  • 打赏
  • 举报
回复
关键在文件的读取上,数据绑定显示不是问题
happyboyxq 2009-08-26
  • 打赏
  • 举报
回复
up
风之影子 2009-08-26
  • 打赏
  • 举报
回复
先把文本文件的内容读取到内存表,最后再绑定.

具体多少等注意一下.

不知道你的文本结构,无法明码

提点小逻辑
chenxiaoping_2009 2009-08-26
  • 打赏
  • 举报
回复
顶个...
sunshine_anycall 2009-08-26
  • 打赏
  • 举报
回复
可以参考下这个,然后差不多就是字符串处理的问题了
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText);
}

// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);

// Open the file to read from.
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
liffe 2009-08-26
  • 打赏
  • 举报
回复
wzuomin 2009-08-26
  • 打赏
  • 举报
回复
帮顶
staticuser 2009-08-26
  • 打赏
  • 举报
回复
哇,这好的帖子不能沉了啊。。。。顶起来


[img=http://pic.nipic.com/2008-05-12/2008512155311339_2.jpg]情人节快乐[/img]
****************************************************************
今天回帖带祝福,七夕情人节快乐~^_^
****************************************************************
加载更多回复(1)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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