C# 数据网格日期字符串筛选器
试图获得关于这个问题的帮助我试图用一个文本框在我的数据网格中找到一个特定的日期。日期当前为格式为dd/MM/yyyy的字符串。在日期列中。我以前发过帖子,但没有得到有用的答案,我的问题也被掩盖了。似乎没有人有答案,他们只是回避这个问题。目前我无法将日期作为DateTime,因为应用程序的其余部分已格式化 谢谢 编辑代码:C# 数据网格日期字符串筛选器,c#,date,datagrid,C#,Date,Datagrid,试图获得关于这个问题的帮助我试图用一个文本框在我的数据网格中找到一个特定的日期。日期当前为格式为dd/MM/yyyy的字符串。在日期列中。我以前发过帖子,但没有得到有用的答案,我的问题也被掩盖了。似乎没有人有答案,他们只是回避这个问题。目前我无法将日期作为DateTime,因为应用程序的其余部分已格式化 谢谢 编辑代码: public class ImagesInfo { public string FileName { get; set; } //For Picture File Na
public class ImagesInfo
{
public string FileName { get; set; } //For Picture File Name
public string Description { get; set; } //For the Description of the Picture
public string Category { get; set; } //Category of Picture
public string Date { get; set; }//Date Taken of the Picture, format discussed in report.
public string Comments { get; set; } //Comments for the picture
}
在datagrid中查找类别时使用的代码
if (categoryFilterBox.Text == string.Empty)
{
//used if nothing is in the filter box to avoid blanking
var source = new BindingSource();
source.DataSource = images;
navigationGrid.DataSource = source;
}
else
{
//making a new filtered list that includes the matching Categorys and binding it.
string catFilter;
try
{
catFilter = categoryFilterBox.Text;
var filteredList = images.Where(item => item.Category == catFilter);
var filterSource = new BindingSource();
filterSource.DataSource = filteredList;
navigationGrid.DataSource = filterSource;
}
catch (FormatException)
{
MessageBox.Show("Must be Words of Letters");
}
}
private void addRecord()
{
var newImage = new ImagesInfo();//new instance
newImage.FileName = fileNameTextBox.Text;
newImage.Category = categoryComboBox.Text;
//try catch for input of the date
try
{
newImage.Date = dateTakenTextBox.Text;
}
catch (FormatException)
{
MessageBox.Show("Date Not Correct Format");
}
try
{
newImage.Description = descriptionTextBox.Text;
}
catch (FormatException)
{
MessageBox.Show("Must user letters and words");
}
try
{
newImage.Comments = commentsTextBox.Text;
}
catch (FormatException)
{
MessageBox.Show("Must use letters and words");
}
images.Add(newImage);//Add instance to the main list
if (editCheckBox.Checked)
{
//Binding the new updated list to the datagrid
var source = new BindingSource();
source.DataSource = images;
navigationGrid.DataSource = source;
}
}
将记录添加到我的列表的示例,该列表是datagrid的源
if (categoryFilterBox.Text == string.Empty)
{
//used if nothing is in the filter box to avoid blanking
var source = new BindingSource();
source.DataSource = images;
navigationGrid.DataSource = source;
}
else
{
//making a new filtered list that includes the matching Categorys and binding it.
string catFilter;
try
{
catFilter = categoryFilterBox.Text;
var filteredList = images.Where(item => item.Category == catFilter);
var filterSource = new BindingSource();
filterSource.DataSource = filteredList;
navigationGrid.DataSource = filterSource;
}
catch (FormatException)
{
MessageBox.Show("Must be Words of Letters");
}
}
private void addRecord()
{
var newImage = new ImagesInfo();//new instance
newImage.FileName = fileNameTextBox.Text;
newImage.Category = categoryComboBox.Text;
//try catch for input of the date
try
{
newImage.Date = dateTakenTextBox.Text;
}
catch (FormatException)
{
MessageBox.Show("Date Not Correct Format");
}
try
{
newImage.Description = descriptionTextBox.Text;
}
catch (FormatException)
{
MessageBox.Show("Must user letters and words");
}
try
{
newImage.Comments = commentsTextBox.Text;
}
catch (FormatException)
{
MessageBox.Show("Must use letters and words");
}
images.Add(newImage);//Add instance to the main list
if (editCheckBox.Checked)
{
//Binding the new updated list to the datagrid
var source = new BindingSource();
source.DataSource = images;
navigationGrid.DataSource = source;
}
}
编辑:我现在应该如何得到它,但它似乎不起作用
if (startDate.Text == string.Empty)
{
var source = new BindingSource();
source.DataSource = images;
navigationGrid.DataSource = source;
}
else
{
string dateFilter = startDate.Text;
var filteredList = images.Where(item => item.Date == dateFilter);
var filterSource = new BindingSource();
filterSource.DataSource = filteredList;
navigationGrid.DataSource = filterSource;
}
试试这个:
DateTime temp;
// try to parse the provided string in order to convert it to datetime
// if the conversion succeeds, then build the dateFilter
if(DateTime.TryParse(TrystartDate.Text, out temp))
{
string dateFilter = temp.ToString("dd/MM/yyyy");
var filteredList = images.Where(item => item.Date == dateFilter);
var filterSource = new BindingSource();
filterSource.DataSource = filteredList;
navigationGrid.DataSource = filterSource;
}
如果我们看到你的代码会更好。请发布。代码已经添加。如果需要更多,我可以提供一个问题,到底是什么问题?我知道你对日期有问题,日期是字符串。但是,我还没有找到你想要的具体日期?感谢在我发布的代码中有类别过滤器,我希望我的日期栏也有相同的内容,但由于它们是字符串,我不知道如何操作,并希望获得有关此问题的帮助。添加了我所拥有的内容,但其工作方式与类别过滤器不同,我假设,因为字符串是数字,它有/?这与我尝试刷新相同datagrid的效果相同,我使用不同的数据类型(不是DateTime而是int或Double)会更好吗?我认为这不会有帮助,因为您使用的字符串是日期。换句话说,我看不出它是如何工作的。