这些代码下面是我试图实现这些目的的方法,但是不幸的是我没有做到这一点,这就是为什么我在这里。我真的需要帮助人员来解决我的这些问题。
预期输出:
如果用户在两个日期时间选择器之间选择一个日期,例如从9月1日到9月21日,那么将仅显示日期为9月1日到21日的行。
这些代码下面什么也没有发生。
注意:
DateReturn和DateReturn2都是datetimepicker,第一个是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;
}
}
}
}
}
答案 0 :(得分:1)
我不确定这是否100%正确,但是主要思想是尝试使用新的数据表将其设置为数据源
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;
}
}
}
}