我想实现这样一个功能,界面上有两个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(这一行不显示)
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(这一行不显示)
解决方案 »
- 求教于C#高手中的深知C#精髓的高手(关于方法的调用问题)
- 求两个正则表达式,数字区间
- vs.net2005 中word 打印问题?
- textbox 如何获得文本的长度(像素单位)
- 乞丐....求救 看我哪里有错 谢谢
- xml导入数据库的问题
- 请问有没有C#的练习题?光理解了概念没用啊,没有实践一切都是白扯。哪位帮帮忙给些练习题做做,要有答案的那种哦,谢谢~~~~~~~~~~~~~
- Reporting Services 安装时为何检测不到已经安装的IIS?(WIN 2003操作系统)
- 如何读oracle表的所有信息
- 有关套打的问题。我在打第一张票据时,位置是对的,可是到了第十几张就相差很远了。纸张是带孔的票据
- 关于截取字符串
- 高手请来,简单送分(100)
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);
}
}
}
嗯,我试过了,在记事本中写入文字,那么我帮楼主提一个思路,既然二楼已经给出了读和写文本文件中的方法,楼主就可以就只用读取字段,然后添加到DataGridView里面,添加到DataSet["Tabel"]里面就行了.
第一种如下:
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 + " 是无效的。略过。");
}
}
}出错:分隔符不能为空,可是将字段两头不需要的字符也显示了!大家快帮忙想想办法!
看帖一定要回的,分也一定要接的。^_^
****************************************************************[/align]
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