2014-04-19 159 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