如何在 C# 中的 DataRow 或 GridView 中显示以逗号分隔的文本文件中的文本
我用 C# 创建了一个应用程序。我想在 DataRow 或 GridView 中显示以逗号分隔的文本文件中的文本。
我正在使用此代码在列表框中显示文本
private void button1_Click(object sender, EventArgs e)
{
var file = File.OpenText("C:\\File.txt");
string line;
bool flag = true;
while ((line = file.ReadLine()) != null)
{
listBox1.Items.Add(new { srno= line, Date= "descr", Time= DateTime.Now ,symbol = Symbol });
}
}
,但其他人无法很好地理解其显示的内容。我想显示类似的内容,
请检查此链接 https://i.sstatic.net/LEmdz.jpg
如果有人可以帮助我,我将不胜感激。
提前致谢。
I have created an application in C#. I want to display the text in the text file separated by commas in a DataRow or a GridView.
I am using this code to display the text in a listbox
private void button1_Click(object sender, EventArgs e)
{
var file = File.OpenText("C:\\File.txt");
string line;
bool flag = true;
while ((line = file.ReadLine()) != null)
{
listBox1.Items.Add(new { srno= line, Date= "descr", Time= DateTime.Now ,symbol = Symbol });
}
}
But its not well for others to understand what its displaying.i want to display something like this
check this link https://i.sstatic.net/LEmdz.jpg
There would be great appreciation if someone could help me.
Thanks In Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
愚蠢的我看起来这是WinForms而不是asp.net。被重新标记了。那我就把这个留给别人了。
您需要将文件转换为
DataTable
。 http://www.akamarketing.com/blog 有一个很好的例子/256-csv-datatable.html这更像是一种通用方法。
这是一个未经测试的示例,您可以尝试解决。
Silly me looks like this is WinForms not asp.net. Got retagged. I'll leave this here for someone else then.
You'll want to turn the file in to a
DataTable
. There is a decent example of this at http://www.akamarketing.com/blog/256-csv-datatable.htmlIt's more of a generic approach than anything.
Here is an untested example you could try to work through.
定义一个类(例如
Foo
),它具有四个公共属性
- srno、Date、Time 和 symbol。使用String.Split
方法解析逗号分隔的字符串,构造 Foo 类的对象并将其附加到List
中。最后将List
对象绑定到GridView
控件。演示:
Define a class (say
Foo
) having four publicproperties
- srno, Date,Time, and symbol. UseString.Split
method to parse the comma separated string, constructs an object of Foo class and append it toList<Foo>
. Finally bind theList<Foo>
object to theGridView
control.Demo: