C# 搜索文本文件中的特定行,添加到预定义的dgv

C# 搜索文本文件中的特定行,添加到预定义的dgv,c#,winforms,datagridview,text-files,datasource,C#,Winforms,Datagridview,Text Files,Datasource,有很多关于如何阅读文本文件等的话题,我觉得我都读过了。但我还没能把它应用到我的问题上 我有一个如下所示的文本文件: ProductID、Product、CustomerID、第1行、第2行、第3行 AABBCC,香蕉,K001,什么,什么,什么 BBCCAA,苹果,K002,一些东西1,一些东西2,一些东西3 AACCBB,橙子,K003,一些4,一些5,一些6 香蕉,K001,什么,什么,什么 我有一个表单(Form1),其中有一个文本框(tbCustomerID)和一个dataGridVie

有很多关于如何阅读文本文件等的话题,我觉得我都读过了。但我还没能把它应用到我的问题上

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

ProductID、Product、CustomerID、第1行、第2行、第3行 AABBCC,香蕉,K001,什么,什么,什么 BBCCAA,苹果,K002,一些东西1,一些东西2,一些东西3 AACCBB,橙子,K003,一些4,一些5,一些6 香蕉,K001,什么,什么,什么

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

我想做的事情是在Form1的文本框中填写CustomerID 然后我想循环查看文本文件中的所有客户,然后在预定义的dataGridView中显示ProductID和Product,共有6列。(ProductID和Product=第1列和第2列)

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

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

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

然后在表格1中

    Form1 frm1 = new Form1();
    private void button_Click(object sender, EventArgs e)
    {
        string aa = tbKundID.Text;
        dgvProducts.Rows.Add(frm1.findCustomer(aa));            
    }
但是通过这种方式,我只能从textfile的第2列中得到一个值,因为我不能在c#中返回两个值。 我还尝试读取文本文件中的所有行,然后划分所有列,然后以某种方式显示一些我想显示的特定内容,但我无法通过搜索或添加到dataGridView来实现这一点。。
这是我最后的选择,因此我非常感谢您的帮助,帮助我以最简单的方式完成这项工作。

编辑您的第一次尝试:

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;

}
Form1 frm1=new Form1();
私有无效按钮\u单击(对象发送者,事件参数e)
{

dgvProducts.DataSource=frm1.ReadFromFile(tbCustomerID.Text);要归功于@monkyxyz解决了数据表方法

就我个人而言,我更喜欢尽可能地保持面向对象,并发现数据表不那么灵活(除非问题范围保持简单和静态)

另一种选择是将文本文件反序列化为对象,并根据需要传递对象

除非你表示需要,否则我将省略所有代码

但为了让事情顺利进行:

  • 这是一个漂亮的小盒子

我现在可以完美地遍历文本文件了!但是如何从文本文件中取出第1列和第2列,并将其添加到具有预定义列的datagridview中?(如果搜索成功,我想取出第1列和第2列)从未听说过Seralization,我对编程相当陌生。但我会去看看的!@Nisse很乐意帮忙!在.net中内置的序列化仅限于二进制(1和0),和XML。因为您使用逗号分隔的值,所以我提供了另一个项目。没有必要重新发明轮子。仅供参考:这里有更多关于XML序列化的内容。谢谢链接!我觉得这个序列化的东西真的很混乱,另一个答案我无法从文本文件中挑出两列,然后在预定义的dgv中显示它们。我真的无法让它按我想要的方式工作=/我不想问,但你介意用代码向我展示一些解决方案吗?(我有一个文本文件,我想在其中搜索customerID,然后在预定义的dgv中只显示文本文件中的productID和产品,并带有列)
    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 frm1 = new Form1();
    private void button_Click(object sender, EventArgs e)
    {
        string aa = tbKundID.Text;
        dgvProducts.Rows.Add(frm1.findCustomer(aa));            
    }
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;

}