tags:

views:

709

answers:

3

Hi all,

My code is below, the code works fine when the strFilterText has a value but if the string is empty then it filters the rows that have no value which are the ones I want to show, for example if strFilterText contains "shoes" then the datagrid shows all the rows that contain "shoes" but if I want to find rows that have no category i.e. the catergory is NULL then strFilterText contains "" and I would like it to return everything with "" in the category but the code below shows me everything with a category if strFilterText is "".

Thanks for the help.

    strFilterText += " LIKE '%" + txtboxValue.Text + "%'";
    performFilter(strFilterText);
}

private void performFilter(string strFilterText)
{
        DataTable table = dataGridView1.DataSource as DataTable;
        if (table != null)
        {
            List<DataRow> filteredRows = new List<DataRow>(table.Select(strFilterText));

            CurrencyManager cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
            cm.SuspendBinding();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Visible = filteredRows.Contains(((DataRowView)row.DataBoundItem).Row);
            }
            cm.ResumeBinding();
        }
}
A: 

Hi,

did you consider using a BindingSource as the DataSource for your DataGridView? It contains a property named "Filter", which you can assign a filter string to.

You will have to change strFilterText, however. To filter for you should use FieldName IS NULL. You may want to try this in your code as well - it might work for table.Select...

Thorsten Dittmar
A: 

You could try setting your filter text differently if the field is empty:

strFilter += String.IsNullOrEmpty(txtboxValue.Text) ? " = ''" : " LIKE '%" + txtboxValue.Text + "%'";
Justin Niessner
A: 

Try to change the first line to:

strFilterText+= string.IsNullOrEmpty(txtboxValue.Text)? " IS NULL" : " LIKE '%" + txtboxValue.Text + "%'";
Grzenio
Would've gone with this...but it sounded like his fields contained an empty string rather than a true null value.
Justin Niessner
Aye, its not really clear is the values are null or "". I am sure he will manage now though.
Grzenio
Both answers worked but this is the one I went with, thanks Grzenio!