如何将过滤器添加到datagridview [英] How add filter to datagridview

查看:113
本文介绍了如何将过滤器添加到datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个csv文件加载到datagridview
,现在我想向datagridview添加过滤功能

I am trying to load a csv file to datagridview and now i want to add filtering to the datagridview

该怎么办?
这是我读取和加载csv文件的方式

How to do? Here's how I read and load csv file

openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Title = "Open CSV Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "CSV";
openFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files(*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
try
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string csvPath = openFileDialog1.FileName;
        string rowValue;
        // int rowValue = int.Parse(??);
        string[] cellValue;
        dataGridView1.Rows.Clear();
        //dataGridView1.Columns.Clear();
        if (System.IO.File.Exists(csvPath))
        {
            System.IO.StreamReader fileReader = new StreamReader(csvPath);
            rowValue = fileReader.ReadLine();
            cellValue = rowValue.Split(',');



            for (int i = 0; i <= cellValue.Count() - 1; i++)
            {
                DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
                column.Name = cellValue[i];    //column name , value
                column.HeaderText = cellValue[i];
                dataGridView1.Columns.Add(column);
                // dataGridView1.Columns[].CellType = typeof(Int64);
                //Conver.ToString
                dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; // Korean? 칼럼 헤더 가운데 정렬
             //   dataGridView1.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
               // dataGridView1.Columns[0].DataPropertyName = "field name";
            }
            while (fileReader.Peek() != -1)
            {
                rowValue = fileReader.ReadLine();
                cellValue = rowValue.Split(',');
                dataGridView1.Rows.Add(cellValue);
            }
            fileReader.Dispose();
            fileReader.Close();`


推荐答案

而不是直接将行添加到 DataGridView 将它们添加到 DataTable 然后将该表设置为 DataGridView DataSource ,然后使用该 table.DefaultView。 RowFilter 过滤 DataGridView

Instead of adding rows directly to DataGridView add them to a DataTable and then set that table as DataSource of your DataGridView, then use that table.DefaultView.RowFilter to filter the DataGridView.

您可以使用以下示例简单地更改代码。

You can simply change your code using below examples.

创建数据表:

var table = new DataTable();

将列添加到数据表:

table.Columns.Add("column name");

将行添加到数据表:

使用范围添加行,例如 string []

To add a row using a range for example a string[]:

table.Rows.Add(range);

将表设置为 DataSource DataGridview

Set the table as DataSource of the DataGridview

dataGridView1.DataSource = table;

使用数据表过滤:

要使用数据表进行过滤,例如仅显示名字 John 的行:

To filter using the data table, for example to show only rows where FirstName is John:

((DataTable)dataGridView1.DataSource).DefaultView.RowFilter = "FirstName = 'John'"; 

了解详情:

  • Creating a DataTable
  • Adding Columns to a DataTable
  • Adding Data to a DataTable
  • DataView.RowFilter and Filter Expression.

这篇关于如何将过滤器添加到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆