Filtering Datagridview Without Changing Datasource

Filtering DataGridView without changing datasource

I just spent an hour on a similar problem. For me the answer turned out to be embarrassingly simple.

(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", textBoxFilter.Text);

How to filter with textbox the datagrid view without datasource

It may be possible filter a datagrid that doesn't have a datasource, but I suspect it isn't.

Regardless, an easier solution would be to just give the grid a datasource. Rather than programmatically adding rows to the datagrid, instead create a DataTable and add rows to it, then set the grid's data source to that table. Now you can use standard filtering methods.

How to filter Datagridview using combobox without changing datasource

You can use a BindingSource to bind your datasource to your DataGridView.
This way you only change the Filter property of your bindingSource and the filter automatically will apply to data shown in the grid.
No need to change datasource.

How add filter to 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.

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.

DataGridView search and filter without Datasource

There may be better ways, but one possibility is to simply apply a Linq Where-statement to refresh your DataSource. For example:

private string folderPath = @"C:\Users\TULPAR\Desktop\elektrik projesi\proje\dosyalar\";

public Form1()
{
this.InitializeComponent();
this.dataGridView1.DataSource = new System.IO.DirectoryInfo(this.folderPath).GetDirectories();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
var src = new System.IO.DirectoryInfo(this.folderPath).GetDirectories().Where(di => di.Name.StartsWith(this.textBox1.Text)).ToArray();
this.dataGridView1.DataSource = src;
}

Here, I used a TextBox.TextChanged event to filter entries to only those where the Name began with my input in the TextBox. I also could have changed my condition to di.Name.Contains(...). Coincidentally, when the TextBox is emptied, all directories show.

You could use a different event, or no event at all, depending on how you wish to accomplish this. The condition is also up to you. The general idea though is to reset your DataSource to your filtered version of the same source.

Filtering an DataGridView that doesn't have databinding

I had this issue a few years ago (before I knew about databindings) and found a bug post at Microsoft, saying that this is confirmed, but the issue will propably not be fixed.

However, there are a few possibilities to solve this.

  1. Insted of adding rows to the datagridview, add rows to a datatable and bind it to the datagridview.

    DataTable table = new DataTable();
    table.Columns.Add("Name", typeof(String));
    table.Columns.Add("...", typeof(String));

    foreach (var element in list)
    table.Rows.Add(element.Name, element.Something);

    dataGridView1.DataSource = table1;
    table.DefaultView.RowFilter = "Name Like '...'";
  2. Create a Class that inherits from BindingList and implements IBindingList. Then bind it to your DataGridView.

  3. Set DataGridView VirtualMode to true.

Method two is more complicated, because you have to add your own logic to implement the FindCore Method.

And you should look here: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/68c8b93e-d273-4289-b2b0-0e9ea644623a



Related Topics



Leave a reply



Submit