Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have patient visit grid,and it has admit date and discharge date column, i want to give a feature to search for a patient with particular admit date, and it needs to be searched with the grid itself, not running a query, i want it to be that way itself,
how do i filter records in the grid with a given admit date

please answer me, thank you

What I have tried:

i'm very new to asp.net, grid operations are clumsy and difficult for me, please suggest some source to learn about grid operaations
Posted
Updated 9-Aug-16 8:53am
Comments
Vincent Maverick Durano 9-Aug-16 14:26pm    
Let me clarify. Are you displaying all the records in the Grid at once?
Member 12677894 9-Aug-16 14:40pm    
no, i have set paging=15
Vincent Maverick Durano 9-Aug-16 14:46pm    
Good. If that's the case then you need to write a query to perform a data request based on your filter.

1 solution

If you are using ADO.NET with DataTable for binding your GridView, then you can do something like this:

C#
protected void BindGrid(string searchText){
    using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
        using(SqlCommand cmd = new SqlCommand(sql,connection)){
                string sql = "SELECT * FROM TableName WHERE YourFieldName = @Param1";
                cmd.Parameters.AddWithValue("@Param1", searchText);
                DataTable dt = new DataTable();
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                ad.Fill(dt);

                if (dt.Rows.Count > 0) { //check if the query returns any data
                       GridView1.DataSource = dt;
                       GridView1.DataBind();
                }
                else
                {
                     //No records found
                }
        }

    }
}

protected void Button1_Click(object sender, EventArgs e){
        BindGrid(YourTextBoxID.Text.Trim());
}


The code above is the typical way to search for a particular records in the database based on a TextBox value. You may need to validate the text to ensure validate dates format. You can also use a MaskEdit or Calendar extender control for your TextBox to ensure that the dates entered is valid. Depending on your datatype, you may also need to do a datatype conversion from string to datetime.

Hope that helps.
 
Share this answer
 
Comments
Member 12677894 9-Aug-16 15:06pm    
i dont want to use query to get results, i want to get results from the grid itself
Vincent Maverick Durano 9-Aug-16 15:28pm    
You would need to use DOM manipulation to do that. You could try using JavaScript/jQuery to implement it, BUT it will only search for the current paged grid. You can't expect to do a client-side DOM manipulation to search with the hidden pages of the grid. If you want an example do a search at google using this term:"Search GridView using JavaScript" good luck!
Member 12677894 9-Aug-16 15:38pm    
user is : India
server:USA
there are two admit dates in database
1)24/06/2016 14:30:00
2)25/06/2016 00:00:00

while displaying i'll convert them to indian equivalent time by adding 9 hour 30 minutes to themm and take a shortdate part of it and display in the grid
so while displaying in the grid 24/06/2016 14:30:00 converted to 25/06/2016 00:00:00 and 25/06/2016 00:00:00 convrted to 25/06/2016 9:30:00 and if we take short date part of them both are 25/06/2016 and both the records diplayed as 25/06/2016

now when user wants to filter the record based upon admit date, he enters 25/06/2016, when we send the input to the query only one record matches and fetches the record,

if we convert the user inputted date 25/06/2016 to USA timing, it would be 24/06/2016 14:30:00 and it'll be matching with 24/06/2016 14:30:00 and not matches with 25/06/2016 00:00:00,
so user cant get records properly, so how do i resolve this problem??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900