Reputation: 59
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:
DateReturn and DateReturn2 is both datetimepicker, the first one is DateReturn
public partial class SIMSSupplier : UserControl
{
ADDSupplier supply;
ADDPReturns returns;
public SIMSSupplier()
{
InitializeComponent();
}
public DataTable dbdataset;
public void CustomDatetime()
{
var now = DateTime.Now;
var first = new DateTime(now.Year, now.Month, 1);
var last = first.AddMonths(1).AddDays(-1);
DateReturn.Value = new DateTime(now.Year, now.Month, 1);
DateReturn2.Value = last;
}
public void ReturnDetails()
{
FillSupplier(PurchaseReturn, "Select ReturnID, Supplier, Itemdescription, Modelno, Srp, Code, Date, Remarks from Purchase_Return");
}
private void FillSupplier(DataGridView grid, string request)
{
using (var con = SQLConnection.GetConnection())
using (var select = new SqlCommand(request, con))
using (var sda = new SqlDataAdapter())
{
dbdataset = new DataTable();
sda.SelectCommand = select;
sda.Fill(dbdataset);
grid.DataSource = new BindingSource() { DataSource = dbdataset };
sda.Update(dbdataset);
}
}
private void SIMSSupplier_Load(object sender, EventArgs e)
{
ReturnDetails();
CustomDatetime();
}
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 '" + DateReturn.Value.ToString() + "' and '" + DateReturn2.Value.ToString() + "'", con))
{
using (var sd = new SqlDataAdapter(select))
{
var dv = new DataView(dbdataset);
PurchaseReturn.DataSource = dv;
}
}
}
}
}
I tried to select Sept 22, but nothing happen
Upvotes: 0
Views: 238
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