当前位置:编程学习 > C#/ASP.NET >>

C# 读取文本内容,在Dev控件中显示数据--<续>

上篇文章:html">http://www.zzzyk.com/kf/201104/88559.html

现在继续,我已经自己解决了自己的两个问题,且有小小改动。

 这是改进后运行的效果:

第一,读取第三行以后额数据并显示。

 简单的就是定义个计数器count,先while循环读取文本,当count为3时才开始真正的读取我们需要的内容。

int count = 0;
while (strLine != null)
{
strLine = sr.ReadLine();
count++;
if (count == 3)
{
while (strLine != null)
{
string[] strArry = strLine.Split( );
DataRow dr = dt.NewRow();
for (int i = 0; i <= strArry.Length - 1; i++)
{
dr[i] = strArry[i];
}
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
break;
}

第二,第二行的数据读取,并作为列名 。这是不行的,至少我现在不知道有何办法。由于前面已经解决我所需要的问题,这也就不再思考了,应该没有解决方案。

 运行效果中有打开文件的Button,并且还有个Textbox(用来显示文件路径)。

 这是打开文件的基本代码:

private void OpenFile()
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Title = "打开";
openFile.FileName = "";//文件名
openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);//默认打开我的电脑
openFile.Filter = "文本文件(*.txt)|*.txt";//文件类型
openFile.ValidateNames = true;//
openFile.CheckFileExists = true;//
openFile.CheckPathExists = true;//
if (openFile.ShowDialog() == DialogResult.OK)
{
// DisplayData(openFile.FileName);
txtFilePath.Text = openFile.FileName;
}

}

这些打开文件的代码,网上有很多,或许有比这更完善的。

更改后的源代码就不在贴出来了,都很简单

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,