【问题标题】:Search specific line in the text file, add to a predefined dgv在文本文件中搜索特定行,添加到预定义的 dgv
【发布时间】:2013-10-10 19:23:32
【问题描述】:

有很多关于如何阅读文本文件等的主题,我觉得我已经阅读了所有主题。但我无法将其应用于我的问题。

我有一个如下所示的文本文件:

ProductID,Product,CustomerID,lineone,linetwo,linethree AABBCC,香蕉,K001,某事,某事,某事 BBCCAA,苹果,K002,something1,something2,something3 AACCBB,橙子,K003,something4,something5,something6 CCAABB,香蕉,K001,某事,某事,某事

我有一个表单 (Form1),其中有一个文本框 (tbCustomerID) 和一个 dataGridView (dgvProducts)。

我想做的是我想在Form1的文本框中填写CustomerID 然后我想遍历文本文件中的所有 CostumerID,然后在具有 6 列的预定义 dataGridView 中显示 ProductID 和 Product。 (ProductID 和 Product = 第 1 列和第 2 列)

我的第一次尝试:(来自普通班级)

    public DataTable ReadFromFile()
    {            
        //Read the data from text file
        string[] textData = File.ReadAllLines("MyTextFile.txt");
        string[] headers = textData[0].Split(',');

        //Create and populate DataTable
        DataTable dataTable1 = new DataTable();
        foreach (string header in headers)
            dataTable1.Columns.Add(header, typeof(string), null);
        for (int i = 1; i < textData.Length; i++)
            dataTable1.Rows.Add(textData[i].Split(','));            

        return dataTable1;

    }

然后在我的 Form1 中:

    Form1 frm1 = new Form1();

    private void button_Click(object sender, EventArgs e)
    {

        dgvProducts.DataSource = frm1.ReadFromFile();            
    }

但这只是将文本文件中的所有项目显示到 dataGridView 中。

尝试 2:(来自我的班级)

    public string findCustomer(string CustomerID)
    {
        StreamReader sr = new StreamReader("MyTextFile.txt");
        string searchId = CustomerID;
        string actualId = "";
        string produktID = "No match found";
        string[] details = null;
        string line = null;
        while ((line = sr.ReadLine()) != null)
        {
            line = line.Trim();
            if (line == "") continue;
            details = line.Split(',');
            actualId = (details[0]);
            if (actualId == searchId)
            {
                productID = details[2].Replace("\"", "");
                break;
            }
        }
        sr.Close();
        return productID;
    }

然后在Form1中

    Form1 frm1 = new Form1();
    private void button_Click(object sender, EventArgs e)
    {
        string aa = tbKundID.Text;
        dgvProducts.Rows.Add(frm1.findCustomer(aa));            
    }

但是通过这种方式,我只能从文本文件的第 2 列中获取一个值,因为我无法在 c# 中返回两个值。 我还尝试从文本文件中读取所有行,然后划分所有列,然后以某种方式显示一些我想显示的特定内容,但我无法通过搜索或添加到 dataGridView.. 这是我最后的手段,所以我非常感谢有关我应该如何以最简单的方式做到这一点的帮助。

【问题讨论】:

    标签: c# winforms datagridview text-files datasource


    【解决方案1】:

    编辑您的第一次尝试:

    Form1 frm1 = new Form1();
    
    private void button_Click(object sender, EventArgs e)
    {
        dgvProducts.DataSource = frm1.ReadFromFile(tbCustomerID.Text);  <-- pass customer id       
    }
    
    public DataTable ReadFromFile(String customerId)
    {            
        //Read the data from text file
        string[] textData = File.ReadAllLines("MyTextFile.txt");
        string[] headers = textData[0].Split(',');
    
        //Create and populate DataTable
        DataTable dataTable1 = new DataTable();
        foreach (string header in headers)
            dataTable1.Columns.Add(header, typeof(string), null);
        for (int i = 1; i < textData.Length; i++)
           if textData[i].Split(',')[2].ToString() == customerId <-- add the row only if customer Id matches
             dataTable1.Rows.Add(textData[i].Split(','));            
    
        return dataTable1;
    
    }
    

    【讨论】:

    • 我现在完美地循环浏览了文本文件!但是如何从文本文件中取出第 1 列和第 2 列并将其添加到具有预定义列的 datagridview 中? (如果我在搜索中获得成功,我想取出第 1 列和第 2 列)
    【解决方案2】:

    感谢 @monkyxyz 解决您的 DataTable 方法。

    就个人而言,我更喜欢尽可能地保持 OOP 并发现 DataTables 不太灵活(除非问题范围仍然简单且静态)。

    另一种选择是将文本文件反序列化为对象,并根据需要传递对象。我假设您很乐意将对象绑定到 DataGridView。

    除非您指出需要,否则我将省略所有代码。

    但是要让事情顺利进行:

    【讨论】:

    • 从未听说过序列化,我对编程还很陌生。但我会检查一下!
    • @Nisse 乐于助人! .net 中的内置序列化仅限于二进制(1 和 0)和 XML。因为您使用逗号分隔值,所以我提供了其他项目。重新发明轮子没有意义。供参考:这里有更多关于 XML 序列化的内容。 Serialize 和 Deserialize
    • 感谢您的链接!我觉得这个序列化的东西真的很令人困惑,另一个答案我不能从文本文件中挑选出两列,然后在预定义的 dgv 中显示它们。我真的无法让它按我的意愿工作=/我不想问,但你介意用代码给我看一些解决方案吗? (我有一个文本文件,我想在其中搜索 customerID,然后在预定义的 dgv 中仅显示文本文件中的 productID 和产品,并带有列)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2017-08-15
    • 1970-01-01
    • 2015-02-18
    • 2015-12-25
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多