Setting FilterExpression on GridView component at runtime in C#

Hello,
I have an application that queries an SQL table for a list of patients. The results of this query are displayed on a GridView component. I have the component coded to populate with patients information at runtime.  I am not sure how to programmatically set the filter expression for any of the columns.  (I can set FilterExpression within aspx but this is not an option for the application and I need to have FilterExpression set/configured at runtime.  Is this possible in C#?

Thank you!

Here is my method:
// I would like to filter the query results based on text entered in a component called Label1.Text to filter [clientLast]
 protected void BindGrid()
        {
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PEIGenericConnectionString"].ConnectionString);
            string strSelect = @"SELECT [clientFirst] AS 'First Name', [clientMI] AS 'MI', [clientLast] AS 'Last Name', CONVERT(VARCHAR, [dob], 111) AS 'DOB' FROM [Individual_Client] WHERE [programNameID] = @programNameID";

            SqlCommand cmd = new SqlCommand(strSelect, connection);
            connection.Open();

            cmd.Parameters.Add("@programNameID", SqlDbType.Int, 2);
            cmd.Parameters["@programNameID"].Value = Convert.ToInt32(Request.QueryString["programNameID"]);

            SqlDataAdapter ad = new SqlDataAdapter(cmd);

            ad.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
                return;  
        }

Open in new window

KCTechNetAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArkCommented:
DataView dv = new DataView(dt);
dv.RowFilter = "yourFilterExpression";
GridView1.DataSource = dv;
GridView1.DataBind();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KCTechNetAuthor Commented:
Hi Ark,
I tried setting my dv.RowFilter property but the gridview component isn't showing up at all.  I might be missing something altogether that I will need to figure out on my own.  Logically, should dv.RowFilter be assigned my filter AFTER DataBind()?  
Thanks for your reply!
ArkCommented:
try DataView dv = dt.DefaultView;
also check you filter expression. Note it should be like sql WHERE clause, ie string parameters, for example, must be in quotes
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.