Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How add filter to datagridview

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

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();`
like image 989
Simkyujin Avatar asked Jan 06 '23 23:01

Simkyujin


1 Answers

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.

Create a DataTable:

var table = new DataTable();

Add Column to DataTable:

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

Add Row to DataTable:

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

table.Rows.Add(range);

Set the table as DataSource of the DataGridview

dataGridView1.DataSource = table;

Filter using DataTable:

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

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

Learn more:

  • Creating a DataTable
  • Adding Columns to a DataTable
  • Adding Data to a DataTable
  • DataView.RowFilter and Filter Expression.
like image 82
Reza Aghaei Avatar answered Jan 18 '23 12:01

Reza Aghaei