如何在 C# 中的 DataRow 或 GridView 中显示以逗号分隔的文本文件中的文本

发布于 2024-12-07 07:30:25 字数 606 浏览 0 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-12-14 07:30:25

愚蠢的我看起来这是WinForms而不是asp.net。被重新标记了。那我就把这个留给别人了。

您需要将文件转换为DataTablehttp://www.akamarketing.com/blog 有一个很好的例子/256-csv-datatable.html

这更像是一种通用方法。

这是一个未经测试的示例,您可以尝试解决。

DataTable dataTable = new DataTable();
dataTable.Columns.Add("Srno");
dataTable.Columns.Add("Date");
dataTable.Columns.Add("Time");
dataTable.Columns.Add("Symbol");

while ((line = file.ReadLine()) != null)
{
    DataRow row = dataTable.NewRow();
    string[] s =  line.Split(',');
    row["Srno"] = s[0];
    row["Date"] = s[1];
    row["Time"] = s[2];
    row["Symbol"] = s[3];
}

//Add to your GridView that is in your aspx file
gridView.DataSource = dataTable;
gridView.DataBind();

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.html

It's more of a generic approach than anything.

Here is an untested example you could try to work through.

DataTable dataTable = new DataTable();
dataTable.Columns.Add("Srno");
dataTable.Columns.Add("Date");
dataTable.Columns.Add("Time");
dataTable.Columns.Add("Symbol");

while ((line = file.ReadLine()) != null)
{
    DataRow row = dataTable.NewRow();
    string[] s =  line.Split(',');
    row["Srno"] = s[0];
    row["Date"] = s[1];
    row["Time"] = s[2];
    row["Symbol"] = s[3];
}

//Add to your GridView that is in your aspx file
gridView.DataSource = dataTable;
gridView.DataBind();
唠甜嗑 2024-12-14 07:30:25

定义一个类(例如 Foo),它具有四个公共属性 - srno、Date、Time 和 symbol。使用 String.Split 方法解析逗号分隔的字符串,构造 Foo 类的对象并将其附加到 List 中。最后将 List 对象绑定到 GridView 控件。

演示:

public class Foo
{
  public string SrNo {get;set;}
  public string Date {get;set;}
  public string Time {get;set;}
  public string Symbol {get;set;}
}

List<Foo> list=new List<Foo>();

while ((line = file.ReadLine()) != null)
{
    string []ar = line.Split(',');
    list.Add(new Foo()
       {
         SrNo=ar[0],
         Date=ar[1],
         Time=ar[2],
         Symbol=ar[3]
       });
}
//Bind the list
dataGridView1.DataSource=list;

Define a class (say Foo) having four public properties - srno, Date,Time, and symbol. Use String.Split method to parse the comma separated string, constructs an object of Foo class and append it to List<Foo>. Finally bind the List<Foo> object to the GridView control.

Demo:

public class Foo
{
  public string SrNo {get;set;}
  public string Date {get;set;}
  public string Time {get;set;}
  public string Symbol {get;set;}
}

List<Foo> list=new List<Foo>();

while ((line = file.ReadLine()) != null)
{
    string []ar = line.Split(',');
    list.Add(new Foo()
       {
         SrNo=ar[0],
         Date=ar[1],
         Time=ar[2],
         Symbol=ar[3]
       });
}
//Bind the list
dataGridView1.DataSource=list;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文