Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

C# fill datagridview from datetimepickers data range on button click.

$
0
0

I am building a program with a search form that takes dates out of two datetimepickers  (FromDTP and ToDTP) and when the button (Searchbtn) is clicked that it will search the database table (ITrack) and populate the datagridview (gridView1) with the data.  This is simular to the issue from http://social.msdn.microsoft.com/Forums/windows/en-US/88812b29-ac6e-48f1-b3ff-f4c66eeccac7/how-to-display-data-in-datagridview-between-a-particular-range-of-date-by-using-datetimepicker-on-a .  I tried following the example and have spent a few days researching it but I can not get it to work.  When clicking on the button it stops to think then comes back with no update.  Bellow is my code:


        private void Searchbtn_Click(object sender, EventArgs e)
        {

            FromDTP.CustomFormat = "MM/dd/yyyy";
            ToDTP.CustomFormat = "MM/dd/yyyy";
            string query = "SELECT * FROM ITrack WHERE Date BETWEEN @SDate AND @EDate";
            //SqlConnection connection = new SqlConnection("Server=localhost" + "Database=ITrack;");
            SqlConnection connection = new SqlConnection("");
            SqlCommand cmd = new SqlCommand(query, connection);
            cmd.Parameters.AddWithValue("@SDate", FromDTP.Value);
            cmd.Parameters.AddWithValue("@EDate", ToDTP.Value);

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();

            connection.Open();
            da.SelectCommand = cmd;
            da.Fill(ds, "ITrack");

            gridView1.DataSource = ds.Tables[0];

            connection.Close();           
        }



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>