Generalized filtering with GridView and SqlDataSource

Discussion in 'ASP .Net' started by kevinpond, Jan 9, 2007.

  1. kevinpond

    kevinpond Guest

    I have a Gridview displaying the results of a stored procedure. The
    stored procedure is constantly changing so the columns displayed in the
    data grid are constantly changing. I'd like to give users the ability
    to filter by the value of any column. This is easily done using the
    FilterExpression property of the DataSource. However, prior to
    creating the filter expression, I need to know if the column they wish
    to filter contains text or numerical values (basically do I surround
    the value to filter for in quotes or not.) Is there any way to
    determine the data type of the underlying data column?
    Any help would be greatly appreciated.
    kevinpond, Jan 9, 2007
    1. Advertisements

  2. To get info on columns, you need to get the schema. With SqlDataSource you
    can get to the schema if you first get to the datatable. This article
    explains how you can get the datatable out of SqlDataSource:

    Once you have the datatable, its Columns collection will take you to the
    column. Column.DataType property will tell your the type of the column. You
    can apply filtering to the datatable rather that to the datasource. It will
    save you a trip to the database. DataTable.Select can return a filtered
    array of rows and you can databind the grid to this array.

    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    Eliyahu Goldin, Jan 9, 2007
    1. Advertisements

  3. kevinpond

    kevinpond Guest

    Thanks for your quick answer! That helped.
    kevinpond, Jan 9, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.