[英]how to get the next coming seven days in my listbox and remove the others?
so far i did that but it doesnt work instead of getting me the specific date it erase all the list items please help 到目前为止,我做到了,但它不能正常工作,而不是让我知道删除所有列表项的特定日期,请帮助
private void button1_Click_1(object sender, EventArgs e)
{
List<RentalCar> listBox1snew = new List<RentalCar>();
for (int i = 0; i < listBox1s.Count; i++)
{
if ((DateTime.Now.Day - listBox1s[i].WOF.Day) <= 7)
{
listBox1.Items.Insert(0, listBox1snew[i]);
}
}
listBox1.DataSource = listBox1snew;//add car to listbox
}
First of all your logic to check for the date should be: 首先,检查日期的逻辑应为:
DateTime.Now.Subtract(listBox1s[i].WOF).Days <= 7
You're setting the ListBox datasource to listBox1snew but you're never Adding anything to listBox1snew! 您将ListBox数据源设置为listBox1snew,但从未将任何内容添加到listBox1snew!
listBox1.Items.Insert(0, listBox1snew[i]);
This makes no sense. 这是没有道理的。
listBox1snew
is empty . listBox1snew
为空 。 I believe you meant listBox1s[i]
. 我相信你的意思是
listBox1s[i]
。
listBox1.DataSource = listBox1snew;
This makes even less sense. 这更没有意义。 First, you're inserting items in
listBox1.Items
and then you override the DataSource
, effectively ignoring the Items
. 首先,您要在
listBox1.Items
插入项目,然后覆盖DataSource
,从而有效地忽略Items
。 Also, you didn't modify listBox1snew
in any way, so it will still be empty ! 另外,您没有以任何方式修改
listBox1snew
,因此它仍然为空 !
What I think you're after: 我认为您追求的是:
listBox1s
to listBox1snew
. listBox1s
复制到listBox1snew
。 DataSource
to listBox1snew
. DataSource
设置为listBox1snew
。 That would look like: 看起来像:
private void button1_Click_1(object sender, EventArgs e)
{
List<RentalCar> listBox1snew = new List<RentalCar>();
for (int i = 0; i < listBox1s.Count; i++)
{
if (DateTime.Now.Subtract(listBox1s[i].WOF).Days <= 7)
{
// Copy from listBox1s to listBox1snew
listBox1new.Add(listBox1s[i]);
}
}
// Use listBox1new as new data source
listBox1.DataSource = listBox1new;
}
Also, you could easily express this filter with LINQ: 另外,您可以使用LINQ轻松表达此过滤器:
private void button1_Click_1(object sender, EventArgs e)
{
listBox1.DataSource = listBox1s.Where(x => DateTime.Now.Subtract(x.WOF).Days <= 7).ToList();
}
DateTime.Now.AddDays(-7) <= listBox1s[i].WOF
You should subtract the two days first then find days difference. 您应该先减去两天,然后再求出天数差。
private void button1_Click_1(object sender, EventArgs e)
{
List<RentalCar> listBox1snew = new List<RentalCar>();
for (int i = 0; i < listBox1s.Count; i++)
{
if ((DateTime.Now - listBox1s[i].WOF).Days <= 7)
{
listBox1snew .Items.Insert(0, listBox1s[i]);
}
}
listBox1.DataSource = listBox1snew;//add car to listbox
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.