【问题标题】:Filtering a datagridview that does not use a DB, but loads contents from text [duplicate]过滤不使用数据库但从文本加载内容的datagridview [重复]
【发布时间】:2023-03-15 13:54:01
【问题描述】:

我正在尝试使我的 datagridview 可搜索,但我似乎无法掌握它。我尝试用谷歌搜索找到解决方案,但我发现了以下内容:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.Cells[0].Value.ToString().ToLower().Contains(searchTextBox.Text.ToLower()) && row.Cells[0] != null)
    {
        row.Visible = true;
    }
    else
    {
        row.Visible = false;
    }
}

上面的代码链接到表单上的一个按钮,在那里显示的内容不多。当我在文本框中输入字符串并单击按钮时,出现以下异常:

与货币经理职位相关的行不能隐藏。

我不知道如何处理这个异常,我尝试了各种更改,但没有任何帮助。

只是给你一个想法,这是我的表单代码(我只有一个表单):

namespace IntegratorReader
{
    public partial class start : Form
    {
        DataTable dt = new DataTable();

    public start()
    {
        InitializeComponent();
    }

    private void bOpenFileDialog_Click(object sender, EventArgs e)
    {
        OpenFileDialog theDialog = new OpenFileDialog();
        theDialog.Title = "Open Text File";
        theDialog.Filter = "TXT files|*.txt";
        theDialog.InitialDirectory = @"C:\";

        if (theDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((theDialog.OpenFile()) != null)
                {
                    string fullPath = theDialog.FileName;
                    string fileName = theDialog.SafeFileName;
                    MessageBox.Show(fullPath);
                    fillGridView(fullPath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

    private void fillGridView (string path)
    {
        var regex = new Regex("\\\"(.*?)\\\"");
        System.IO.StreamReader file = new System.IO.StreamReader(path);
        string[] columnnames = file.ReadLine().Split(',');

        foreach (string c in columnnames)
        {
            dt.Columns.Add(c);
        }
        string newline;
        while ((newline = file.ReadLine()) != null)
        {
            newline = regex.Replace(newline, m => m.Value.Replace(',', ' '));
            DataRow dr = dt.NewRow();
            string[] values = newline.Split(',');
            for (int i = 0; i < values.Length; i++)
            {
                string v = values[i];
                v = v.Replace("\"", "");
                dr[i] = v;
            }
            dt.Rows.Add(dr);
        }
        file.Close();
        dataGridView1.DataSource = dt;

    }

    private void searchButton_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[0].Value.ToString().ToLower().Contains(searchTextBox.Text.ToLower()) && row.Cells[0] != null)
                {
                    row.Visible = true;
                }
                else
                {
                   row.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
    }
}
}

简短示例:

我打开一个包含很多行的 .txt 文件,而我取第一行并将列作为基础。然后我继续阅读这些行并将它们放入基于“,”拆分器的列中。我意识到这可以通过某种 CSV 阅读器完成,但我需要它来工作。这是一个工作相关的事情,所以我不能给你确切的 .txt 文件,但它通常看起来像这样,只是有更多的行和更多的列:

"客户 ID","客户名称"

“1”,“鲍勃”

"2","丹尼斯"

“3”,“理查德”

"4","费尔南多"

你大概明白了......

现在,我的问题是,关于如何在 datagridview 中搜索,我是完全错误的,还是我正在监督的只是一个较小的问题? 有没有更好的方法在 datagridview 中搜索,如果有,如何?

作为参考,这是表单的外观:

【问题讨论】:

  • 对于错误我更喜欢Moory的解决方案here

标签: c# .net winforms datagridview


【解决方案1】:

问题与this非常重复:

您的代码有两个问题,1- 它不检查空行(最后一行),并且在隐藏行时不暂停 currencyManager。

要解决此问题,请将您的 searchButton_Click 代码替换为以下代码:

private void searchButton_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                currencyManager1.SuspendBinding();

                if (row.IsNewRow)
                    continue;


                if (row.Cells[0].Value.ToString().ToLower().Contains(searchTextBox.Text.ToLower()) )
                {
                    row.Visible = true;
                }
                else
                {
                   row.Visible = false;
                }                
                currencyManager1.ResumeBinding();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
    }

**答案更新**

另一个选项是在删除或隐藏一行之前将当前单元格设置为空,因为错误告诉“

行与货币管理器相关联

"。

if(dataGridView1.CurrentCell!=null)
dataGridView1.CurrentCell = null;

 row.Visible = true;//row.Visible = false

【讨论】:

  • 它会起作用,但 Moory 的解决方案要简单得多。
猜你喜欢
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多