maximus
maximus

Reputation: 4302

How to filter with textbox the datagrid view without datasource

The same question as here: question

But in my case my dataGridView1.DataSource is null. I use the dataGridView1.Rows.Add function to add rows to the table. Is it possible to add the filter the column of dataGridView using textbox without DataSource?

Upvotes: 2

Views: 8437

Answers (5)

vr_driver
vr_driver

Reputation: 2085

What @Rakesh Roa M did was good, but I took it a step further to firstly make it work in VB.NET, but also so that it was case insensitive. Like you, I'm adding content programmatically as well. It's certainly not the best way to do this if you have thousands of entries on a DataGridView due to it's inefficiencies, but if the record set is in the hundreds, this should work fine.

    If TextBox1.Text IsNot String.Empty Then

        For Each row As DataGridViewRow In DataGridView1.Rows

            If row.Cells("your_column_name").Value.ToString().ToUpper().Contains(TextBox_filter.Text.ToUpper().Trim()) Then
                row.Visible = True
            Else
                row.Visible = False                     
            End If
        Next
    End If

Upvotes: 0

Ramgy Borja
Ramgy Borja

Reputation: 2458

you can also try linq

        private void filter()
        {
            if (this.txtsearch.Text != string.Empty)
                 this.dataGridView1.Rows.OfType<DataGridViewRow>().Where(r => r.Cells["column_name"].Value.ToString() == this.txtsearch.Text.Trim()).ToList().ForEach(row => { if (!row.IsNewRow) row.Visible = false; });
            else
                 this.dataGridView1.Rows.OfType<DataGridViewRow>().ToList().ForEach(row => { if (!row.IsNewRow) row.Visible = true; });

        }

Upvotes: 1

Rakesh Rao M
Rakesh Rao M

Reputation: 41

Or you can use this code:

if (textBox1.Text != string.Empty)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[column_index].ToString().Trim().Contains(textBox1.Text.Trim()))
                    {
                        row.Visible = true;
                    }
                    else
                        row.Visible = false;
                }
            }

Upvotes: 3

Rakesh Rao M
Rakesh Rao M

Reputation: 41

Insted of adding rows to the datagridview, create a DataTable and add rows to it and bind it to the datagridview. Now you can use TextBox to search.

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

foreach (var element in list)
   table.Rows.Add(element.Column_Name1, element.Column_Name2, ...);

dataGridView1.DataSource = table;
table.DefaultView.RowFilter = "Column_Name1 Like '"+TextBox.Text+"'";

Upvotes: 0

ean5533
ean5533

Reputation: 8994

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.

Upvotes: 0

Related Questions