简体   繁体   English

绑定源过滤器无法正常工作

[英]Bindingsource filter is not working right

I need to filter ADGV between dates with datepicker1 and datepicker2, but my code only filter Day, no matter the month or year it filters between DAYS only. 我需要使用datepicker1和datepicker2在日期之间过滤ADGV,但是我的代码仅过滤Day,无论其仅在DAYS之间过滤的月份或年份。 I tried to messagebox my var values : string with 3/1/2018 datetry is 01.03.2018 datepicker1 is 01.05.2019 datepicker2 is 27.05.2019 and filter looks like this Data >= '01.05.2019' AND Data < '27.05.2019' and i don't get first day in the list, list starts from day 2 (3/2/2018). 我试图给我的var值添加消息框:日期为3/1/2018的字符串为01.03.2018 datepicker1为01.05.2019 datepicker2为27.05.2019且过滤器看起来像这样Data> = '01 .05.2019'AND Data <'27 .05.2019 '并且我没有进入列表的第一天,列表从第二天开始(3/2/2018)。 I tried in the csv file to change format to 3.1.2018 but that didin't help. 我试图在csv文件中将格式更改为3.1.2018,但这并没有帮助。

PS I'm taking my data from .csv file, no database is used so i can't use sql query. PS我从.csv文件中获取数据,没有使用数据库,所以我不能使用sql查询。

I tried to google it, found similar problems and solutions but none of it was exact like me and i can't adopt it to my code, because i'm not good with programming but i need to make this "filter data" program. 我试图用Google搜索它,发现了类似的问题和解决方案,但是没有一个像我一样精确,我无法将其应用到我的代码中,因为我不太擅长编程,但是我需要制作这个“过滤数据”程序。

string line;
 while ((line = sr.ReadLine()) != null)
 {
 if (!(line.Contains("#")))
{
 string[] columns = line.Split(';');
string datynski = columns[0];
DateTime dateTry = DateTime.ParseExact(datynski,"M/d/yyyy",CultureInfo.InvariantCulture);
datatable1.Rows.Add(dateTry.ToShortDateString(), columns[1], columns[2], columns[3]);
}
bindingsource1.DataSource = datatable1;
bindingsource1.Filter = "Data >= '" + dateTimePicker1.Value.Date + "' and Data <= '" + dateTimePicker2.Value.Date + "'";
adgv.DataSource = bindingsource1;
}

Transforming to datetime should be easy . 转换为日期时间应该很容易

The filter could be like this (using LinQ): 过滤器可能是这样的(使用LinQ):

public static List<DateTime> FilterDatesBetween(List<DateTime> dates, 
DateTime start, DateTime end)
{
    return dates.Where(date => IsDateInPeriod(date, start, end)).
                        OrderBy(date => date).ToList();
}

public static bool IsDateInPeriod(DateTime date, DateTime start, DateTime end)
{
    return (date > start && date < end);
}

try this: 尝试这个:

string line;
while ((line = sr.ReadLine()) != null) {
 if (!(line.Contains("#"))) {
    string[] columns = line.Split(';');

    //this two line are to make sure we have date on the field if you sure its date you can just use this: DateTime.Parse(columns[0]).ToString("yyyy-MM-dd");
    DateTime dateTry = new DateTime();
    DateTime.TryParse(columns[0], out dateTry);

    datatable1.Rows.Add(dateTry.ToString("yyyy-MM-dd"), columns[1], columns[2], columns[3]);    
}                   
bindingsource1.DataSource = datatable1;
adgv.DataSource = bindingsource1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM