【问题标题】:Search for a string in a DataGridView在 DataGridView 中搜索字符串
【发布时间】:2012-03-23 05:12:27
【问题描述】:

我正在通过以下代码将 CSV 加载到 DataGridView 中

        string rowValue;
        string[] cellValue;

        if(File.Exists(textBoxFilePath.Text.ToString().Trim()))
        {
        StreamReader streamReader = new StreamReader(textBoxFilePath.Text.ToString().Trim());
        rowValue = streamReader.ReadLine();
        cellValue = rowValue.Split(',');

        for (int i = 0; i <= 4 - 1; i++)
        {
            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
            column.Name = cellValue[i];
            column.HeaderText = cellValue[i];
            dataGridView1.Columns.Add(column);
        }
        // Reading content
        while (streamReader.Peek() != -1)
        {
            rowValue = streamReader.ReadLine();
            cellValue = rowValue.Split(',');
            DateTime dt = new DateTime();
            dt = DateTime.Now;
            String month = dt.Month.ToString();
            String day = dt.Day.ToString();
            if (dt.Month < 10)
            {
                month = "0" + dt.Month;
            }

            if (dt.Day < 10)
            {
                day = "0" + dt.Day;
            }
            String stringDate = dt.Year.ToString() + "/" + month + "/" + day;
            Console.WriteLine(dt.ToShortDateString());
            Console.WriteLine(stringDate);
            if (cellValue[2].CompareTo(stringDate) == 0)
            {
                dataGridView1.Rows.Add(cellValue);                    
            }
        }
        streamReader.Close();
        dataGridView1.Sort(this.dataGridView1.Columns["User-Name"], ListSortDirection.Ascending);
        }
        else 
        {
            MessageBox.Show("No File is Selected");
        }

我想搜索用户名。这是第 2 列。

【问题讨论】:

    标签: c# search datagrid csv datagridview


    【解决方案1】:

    您是否正在寻找 User-Name 列的特定值?如果是这样,你可以这样处理:

    string searchedText = "some name";
    var matchedRows = this.dataGridView1.Rows
        .Cast<DataGridViewRow>()
        .Select(r => r.Cells["User-Name"].Value)
        .Where(v => v != null && v.ToString() == searchedText)
        .ToList();
    

    matchedRows 列表将包含与您的搜索字符串匹配的所有行。请注意,您需要为 .Cast.Where 方法包含 using System.Linq; 命名空间。

    【讨论】:

    • 我在 ".Where(r => r.Cells["User-Name"].Value.ToString() == searchedText)" 上得到一个 NullReferenceExpection
    • @CocoaDev:如果您的单元格可以为空,您可以从.Where 中删除.ToString()
    • 我的单元格不应该为空,但这不起作用。我尝试了部分匹配但没有结果(即使我在数据网格中看到它们)。我还复制并粘贴了单元格中的数据并进行了搜索。
    • 其他行会怎样?我想搜索一个名称,然后过滤结果。当我清除 TextBox 时,DataGridView 应该显示所有结果(过滤之前)
    • @CocoaDev:一旦将dataGrid.Rows 设置为新值,旧值就会丢失。理想情况下,您希望将原始(因为它是从文件中加载的)集合保留在某处作为备份,当 TextBox 值更改时使用 LINQ 过滤该集合,构建新行并将其设置为网格。或者使用数据绑定filtering mechanism。这两者都需要对您的代码进行一些重构。
    【解决方案2】:

    您可以按照以下模式在DataGridView 中访问任意单元格的value

    dataGridView.Rows[rowIndex].Cells[columnIndex].Value

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 2019-10-06
      • 2012-07-23
      相关资源
      最近更新 更多