How add filter to datagridview
14,429
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.
Author by
Simkyujin
Updated on June 09, 2022Comments
-
Simkyujin almost 2 years
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();`
-
Simkyujin over 8 yearsemm....Is that mean...first csv files adds to Datatable...and then add DataGridview?
-
Reza Aghaei over 8 yearsFirst add columns and rows to
DataTable
then set the data table asDataSource
of grid. This is using data binding to show data in grid. -
Simkyujin over 8 yearsI want to use button_click...and text_box....for example.. i want to see certain Rows_group...so, write row's name in text_box...and then button click how to..?
-
Reza Aghaei over 8 yearsI suppose you have a
FirstName
column, you can have efirstNameTextBox
and inbutton1_Click
, to show rows that haveFirstName
equals to what you typed, use this as filter expression:string.Format("FirstName = '{0}'", firstNameTextBox.Text)
-
Reza Aghaei over 8 yearsto show rows that their
FirstName
contains (like) to what you typed, use this as filter expression:string.Format("FirstName LIKE '%{0}&'", firstNameTextBox.Text)