Anonymous
Anonymous

Reputation: 59

C# Fill Datagridview Between 2 Datetimepicker selected value

Below these code is what i've tried to achieve these but unfortunately i failed to do that, that's why i'm here. I really need help guys in solving these problem of mine.

Expected Output:
If user select a date between two datetimepicker, for example is from Sep 1 to Sep 21, so the row with the date of Sep 1 to 21 will be only displayed.

Nothing Happens here below these code.

NOTE:

enter image description here I tried to select Sept 22, but nothing happen

Upvotes: 0

Views: 238

Answers (1)

chopperfield
chopperfield

Reputation: 567

I'm not sure if this 100% correct, but the main idea is try using new datatable to set it as datasource

private void DateReturn2_ValueChanged(object sender, EventArgs e)
 {
    using (var con = SQLConnection.GetConnection())
    {
         using (var select = new SqlCommand("Select * from Purchase_Return where Date between @date1 and @date2", con))
        {
            select.Parameters.Add("@date1",SqlDbType.Date).value= DateReturn1.Value;
            select.Parameters.Add("@date2",SqlDbType.Date).value= DateReturn2.Value;

            using (var sd = new SqlDataAdapter(select))
            {                    
                DataTable newDT= new DataTable();
                sd.selectcommand = select;
                sd.fill(newDT);
                //PurchaseReturn.DataSource = null;
                PurchaseReturn.DataSource = newDT;        
            }
        }
    }
 }

Upvotes: 1

Related Questions