2014-04-19 8 views
0

이 문제에 대한 도움을 얻으 려하면 텍스트 상자가있는 Datagrid에서 특정 날짜를 찾으려고합니다. 날짜는 현재 dd/MM/yyyy 형식의 문자열입니다. 날짜 열에. 전에 게시했지만 유용한 답변이없고 내 질문이 묻혔습니다. 아무도 그 주제를 피할 수있는 대답을 가진 것처럼 보이지 않습니다. 현재 응용 프로그램의 나머지 형식 때문에 DateTime 같은 날짜를 가질 수 없습니다.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"); 
    } 
} 

DataGrid의 원본 인 내 목록에 레코드가 추가되는 예입니다.

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

이것은 동일한 데이터 격자를 새로 고치려고했던 것과 같은 효과가 있습니다. 다른 Datatype을 DateTime이 아닌 int 또는 Double로 사용하는 것이 더 낫겠습니까? – user3330371

+0

당신이 가진 문자열이 날짜이기 때문에 이것이 도움이 될 것이라고 생각하지 않습니다. 다른 말로 표현하면, 어떻게 이런 식으로 작동하는지 보지 못합니다. – Christos