2014-04-19 160 views
0

试图获得这个问题的帮助即时通讯我试图找到一个特定的日期在我的Datagrid与文本框。目前日期格式为dd/MM/yyyy。在日期栏中。我之前发布过,但没有得到有用的答案,我的问题被埋没了。似乎没有人回答他们只是回避这个问题。目前,由于应用程序的其余部分已格式化,因此我无法将日期设为DateTime。DataGrid日期字符串过滤器

感谢

编辑代码:寻找DataGrid中的一个类别,当我使用

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  
} 

代码。

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; 

     } 
+0

会更好,如果我们看到你的代码。请张贴它。 –

+0

如果需要更多代码,我已经添加了代码我可以提供 – user3330371

+0

有一个问题,究竟是什么问题?我已经知道你对日期有问题,这是字符串。然而,我没有得到你想要找到具体日期的地方吗?谢谢 – Christos

回答

0

试试这个:

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; 
} 
+0

这与我试图刷新同一个数据网格的效果相同,会更好一些,我使用了不同的数据类型DateTime但是int或Double? – user3330371

+0

我不认为这会有所帮助,因为你有的字符串是日期。换句话说,我不明白它是如何工作的。 – Christos