Search button to filter a DataGrid

Discussion in 'ASP .Net' started by thebison, Mar 5, 2006.

  1. thebison

    thebison Guest

    Hi all,

    I hope someone can help with this relatively simple problem.
    I am building a timesheet application using ASP.NET C# with Visual
    Studio 2003.As it is only a protoype application, my database has been
    made in MSDE.

    I have a 'View Resources' page, the purpose of which is to show all
    employees, taken from that table in the database. This works fine, up
    to a point. I have a DataGrid, which is done with a DataSet and a
    DataAdapter. I now want the user to be able to 'filter' the DataGrid,
    using a text box for input, and a 'search' button. I have tried
    different ways of doing this but can't get it to work. I imagine that
    it is achieved by running some kind of different SQL statement (with a
    % LIKE % operator) in the btnSearch_Click method, and re-binding the
    Data Grid after the filter has finished.

    So for example the user could enter 'John' in the 'First Name' text box
    and click 'search', and the Data Grid would refresh accordingly with
    all records LIKE 'John'.

    My Page_Load code looks like this currently
    -------------------------------------------------------------------------
    this.sqlDataAdapter1.Fill(this.dsResources1);
    DataGrid1.DataBind();
    sqlConnection2.Close();

    this.DataGrid1.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GridPageIndexChanged);
    --------------------------------------------------------------------------------------------

    Hope someone can help, I'm sure the btnSearch_Click method is where I
    need to put the code.

    Thanks in advance,

    Al
    thebison, Mar 5, 2006
    #1
    1. Advertising

  2. thebison

    Terry Burns Guest

    DataSet11.Employees.DefaultView.RowFilter = " Firstname like '%" &
    TextBox1.Text & "%' "


    --
    Terry Burns
    http://TrainingOn.net


    "thebison" <> wrote in message
    news:...
    >
    > Hi all,
    >
    > I hope someone can help with this relatively simple problem.
    > I am building a timesheet application using ASP.NET C# with Visual
    > Studio 2003.As it is only a protoype application, my database has been
    > made in MSDE.
    >
    > I have a 'View Resources' page, the purpose of which is to show all
    > employees, taken from that table in the database. This works fine, up
    > to a point. I have a DataGrid, which is done with a DataSet and a
    > DataAdapter. I now want the user to be able to 'filter' the DataGrid,
    > using a text box for input, and a 'search' button. I have tried
    > different ways of doing this but can't get it to work. I imagine that
    > it is achieved by running some kind of different SQL statement (with a
    > % LIKE % operator) in the btnSearch_Click method, and re-binding the
    > Data Grid after the filter has finished.
    >
    > So for example the user could enter 'John' in the 'First Name' text box
    > and click 'search', and the Data Grid would refresh accordingly with
    > all records LIKE 'John'.
    >
    > My Page_Load code looks like this currently
    > -------------------------------------------------------------------------
    > this.sqlDataAdapter1.Fill(this.dsResources1);
    > DataGrid1.DataBind();
    > sqlConnection2.Close();
    >
    > this.DataGrid1.PageIndexChanged += new
    > System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GridPageIndexChanged);
    > --------------------------------------------------------------------------------------------
    >
    > Hope someone can help, I'm sure the btnSearch_Click method is where I
    > need to put the code.
    >
    > Thanks in advance,
    >
    > Al
    >
    Terry Burns, Mar 5, 2006
    #2
    1. Advertising

  3. thebison

    thebison Guest

    Hi Terry,
    Thanks for your quick reply. I have tried that code, with some
    variations but still cannot quite get the search button to have any
    effect on my Data Grid when pressed? In the code below, I have
    simplified the requirement to simply return all records with a DeptID
    less than 2. Unfortunately it still returns all records in the table?
    (Note I am running the Search on the 'Dept' table now, not 'Resource')
    -------------------------------------------------------------------
    private void btnSearch_Click(object sender, System.EventArgs e)
    {
    DataView dv = dsDept1.Tables[0].DefaultView;
    dv.RowFilter = "DeptID <2";
    DataGrid1.DataSource = dv;
    DataGrid1.DataBind();
    }
    ----------------------------------------------------------------------------

    Any suggestions? I'm sure I am close to getting it to work! Is it
    something to do with the fact I have another DataGrid1.DataBind(); in
    my Page_Load section of the code?

    Thanks again!
    thebison, Mar 6, 2006
    #3
  4. thebison

    carl Guest

    Al,

    Try this... place a DataView on your page designer, set the Table to
    the appropriate table in your DataSet and use the DataView as the
    Grid's datasource (you need to add the tables to your DataSet, if you
    haven't already).

    Then you can use this code like this in your Click event:

    newDataView.RowFilter = "DeptID <2";
    DataGrid1.DataBind();

    I didn't look into the problem you're having, but I suspect the problem
    is related to binding to the DataSet in the PageLoad and changing it to
    the DataView in the click method. I think you'll find a DataView
    created at design makes RowFilters and sorting expressions easier to
    use anyway.

    Hope this helps,
    Carl
    carl, Mar 6, 2006
    #4
  5. thebison

    thebison Guest

    Carl,

    That works just right! Thanks!

    Would you know how I can manipulate the RowFilter to return the rows
    based on a user-input textbox?

    I've tried something like

    dataView1.RowFilter = "DeptName LIKE '%'+txtDeptName.text+'%'";

    But this does not work.

    Any suggestions?

    Thanks for your help

    Al
    thebison, Mar 7, 2006
    #5
  6. thebison

    thebison Guest

    Extra info - the error message for the code above is

    Exception Details: System.Data.EvaluateException: Cannot find column
    [txtDeptName.text].

    This occurs when I click on the 'Search' Button

    Thanks!
    thebison, Mar 7, 2006
    #6
  7. thebison

    Terry Burns Guest

    I showed you how to do this, in my first post, which by the way is a live
    working example.

    --
    Terry Burns
    http://TrainingOn.net
    "thebison" <> wrote in message
    news:...
    > Extra info - the error message for the code above is
    >
    > Exception Details: System.Data.EvaluateException: Cannot find column
    > [txtDeptName.text].
    >
    > This occurs when I click on the 'Search' Button
    >
    > Thanks!
    >
    Terry Burns, Mar 7, 2006
    #7
  8. thebison

    thebison Guest

    Terry,

    Sorry, I confused myself with this! I tried the code you posted, and it
    does exactly what I wanted! Thanks a lot!

    The next step for me is to apply this principle to make a search on
    another form, holding lists of Time Periods with Start and End Dates.
    With this search, I need to to possibly search by values from TWO user
    text boxes. So in theory the user could search for values between Start
    Date 11/01/05 and End Date 30/01/05 OR just search based on ONE of the
    two input text boxes. So if the textbox is blank the search will just
    ignore it. Does that make sense?

    I've googled it, but can't quite find what I'm after

    Any help much appreciated.

    Al
    thebison, Mar 7, 2006
    #8
  9. thebison

    carl Guest

    I think you accidently wrapped txtDeptName.Text in double quotes, so
    the literal value is being passed rather than the Text property. Try
    this:

    dataView1.RowFilter = "DeptName LIKE '%'" + txtDeptName.text + "'%'";

    or...

    dataView1.RowFilter = string.Format("DeptName LIKE '%{0}%'",
    txtDeptName.text);

    Glad this worked for you,
    Carl
    carl, Mar 7, 2006
    #9
  10. thebison

    Terry Burns Guest

    Yes, the SQL syntax is

    SELECT column_name FROM table_name
    WHERE column_name
    BETWEEN value1 AND value2

    I have not tried using this in a table, so I would be interested to see if
    it works.

    = "column_name BETWEEN 01/01/2005 AND 01/01/2006"

    let me know

    --


    Terry Burns
    http://TrainingOn.net
    "thebison" <> wrote in message
    news:...
    > Terry,
    >
    > Sorry, I confused myself with this! I tried the code you posted, and it
    > does exactly what I wanted! Thanks a lot!
    >
    > The next step for me is to apply this principle to make a search on
    > another form, holding lists of Time Periods with Start and End Dates.
    > With this search, I need to to possibly search by values from TWO user
    > text boxes. So in theory the user could search for values between Start
    > Date 11/01/05 and End Date 30/01/05 OR just search based on ONE of the
    > two input text boxes. So if the textbox is blank the search will just
    > ignore it. Does that make sense?
    >
    > I've googled it, but can't quite find what I'm after
    >
    > Any help much appreciated.
    >
    > Al
    >
    Terry Burns, Mar 7, 2006
    #10
  11. thebison

    deedivs

    Joined:
    Oct 25, 2006
    Messages:
    1
    Datagrid serach results

    Hi,
    Thanks everyone for you inputs here.I tried usinmg this to serach my grid, but am not getting any results displayed.

    1.I have a list of names displayed on my grid.When the user types in a name in the textbix & clicks the button I do this
    Dim strfil As String
    strfil = "Description like '% " & TextBox1.Text & " %' "
    binddata(strfil)

    2.in my binddata() i do this
    MyDVM = New DataView(xmlds.Tables(0))

    If strfil = "" Then
    Else
    MyDVM.RowFilter = strfil
    End If



    DataGrid1.DataSource = MyDVM


    DataGrid1.DataBind()

    but after the click, there is nothing displayed on my grid, even though I have that name in the resultset.
    Any help???
    deedivs, Oct 25, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jeffreycope

    Search / Filter Records

    jeffreycope, Jul 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    393
    jeffreycope
    Jul 25, 2003
  2. =?Utf-8?B?QUo=?=

    Search Filter!

    =?Utf-8?B?QUo=?=, Oct 31, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    493
    =?Utf-8?B?RWx0b24gVw==?=
    Nov 1, 2005
  3. JScoobyCed
    Replies:
    0
    Views:
    939
    JScoobyCed
    Oct 15, 2004
  4. zax75
    Replies:
    1
    Views:
    1,094
  5. Abby Lee
    Replies:
    5
    Views:
    393
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page