2017-10-08 62 views
0

如果我的標題不好,它的1:30上午,我全部用完咖啡,我很抱歉。我已經嘗試了幾個類似問題的解決方案,但我無法弄清楚,因爲我的情況有點不同。C#DataGridView沒有從單獨的表單填充

我想要使用一個單獨的窗體來選擇參數的方法,我用它來格式化數據傳遞到原始窗體DataGridView,但它不填充DataGridView。我已經將新窗體設置爲對話框,它在被調用時接收到原始窗體引用,窗體上有一個DateTimePicker和一個按鈕,當單擊該按鈕時它調用一個獲取日期時間值的方法,然後它調用原始表單上的方法將datetime參數傳遞給它並關閉對話框。原始表單上的方法使用傳遞給DataGridView的數據的參數運行,然後調用傳遞綁定列表的數據源方法。

這種使用對話框填充DataGridView的方法是我最好的解釋,它是如何解釋在這個網站的類似問題,但它沒有填充我的DataGridView。任何幫助將不勝感激。

這裏是我的代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     SearchDialog search = new SearchDialog(this); 
     search.Show(); 
    } 

)BUTTON2是一個取消按鈕(

public partial class SearchDialog : Form 
{ 
    static DirectoryInfo DexFolder = new DirectoryInfo(Properties.Settings.Default.DexFolderPath); 
    static DirectoryInfo ExcelFile = new DirectoryInfo(Properties.Settings.Default.ExcelFilePath); 

    public SearchDialog(Main form) 
    { 
     InitializeComponent(); 
     fromDateSelector.Checked = false; 
     toDateSelector.Checked = false; 
     MainForm = form; 
    } 

    public Main MainForm {get; set;} 

    private void button2_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     SearchParameters(); 
     Close(); 
    } 

    private void SearchParameters() 
    { 
     DateTime allTime = DateTime.Now.AddYears(-150); 
     DateTime current = DateTime.Now; 
     if (fromDateSelector.Checked == true) 
     { 
      allTime = fromDateSelector.Value; 
     } 
     if (toDateSelector.Checked == true) 
     { 
      current = toDateSelector.Value; 
     } 
     MainForm.GetFiles(DexFolder, current, allTime); 
    } 
} 

主要形式(背面)

public void GetFiles(DirectoryInfo FilePath, DateTime from, DateTime to) 
    { 
     List<string> DexFileNames = new List<string>(); 
     List<string> DexData = new List<string>(); 
     IList<FileManagerView> fileManagerData = new BindingList<FileManagerView>(); 

     string[] ExcelData = File.ReadAllLines(ExcelFile.ToString()); 

     foreach (FileInfo fileInfo in FilePath.GetFiles("*.dex")) 
     { 
      DexFileNames.Add(fileInfo.Name); 
     } 

     foreach (string DexFileName in DexFileNames) 
     { 
      DateTime dexDate = File.GetCreationTime(FilePath + DexFileName); 
      string[] NameData = DexFileName.Split('_', '-', '.'); 
      if (NameData.Length > 2) 
      { 
       dexDate = DateTime.ParseExact(NameData[1] + NameData[2], "yyyyMMddHHmmss", CultureInfo.InvariantCulture); 
      } 
      string DexPHYSID = NameData[0]; 
      string machineNumber = ""; 
      string machineLocation = ""; 
      string telemetryDevice = ""; 
      string routeNumber = ""; 
      string machinePHYSID = ""; 
      string driverName = ""; 

      foreach (string line in ExcelData) 
      { 
       string[] lineData = line.Split(','); 
       if (DexPHYSID == lineData[14].Trim('"')) 
       { 
        machinePHYSID = lineData[14].Trim('"'); 
        machineNumber = lineData[0].Trim('"'); 
        machineLocation = lineData[2].Trim('"'); 
        string RouteNumberFull = lineData[17].Trim('"'); 
        string[] DriverName = lineData[18].Trim('"').Split('('); 
        telemetryDevice = lineData[8].Trim('"'); 
        string[] RouteNumberData = RouteNumberFull.Split(' '); 
        driverName = DriverName[0]; 
        try 
        { 
         routeNumber = RouteNumberData[1] + " " + RouteNumberData[2]; 
        } 
        catch 
        { 

        } 
       } 
      } 

      if (DexPHYSID == machinePHYSID) 
      { 
       FileManagerView fileManagerView = new FileManagerView(); 


       if (dexDate.ToString("dd-MM-yy") == from.ToString("dd-MM-yy") && dexDate.ToString("dd-MM-yy") == to.ToString("dd-MM-yy")) 
       { 
        fileManagerView.machineNumber = machineNumber; 
        fileManagerView.machineLocation = machineLocation; 
        fileManagerView.telemetryDevice = telemetryDevice; 
        fileManagerView.physid = DexPHYSID; 
        fileManagerView.routeNumber = routeNumber; 
        fileManagerView.date = dexDate; 
        fileManagerView.driver = driverName; 
        fileManagerData.Add(fileManagerView); 
       } 
      } 
     } 

     FileManagerPopulate(fileManagerData); 
    } 

    public class FileManagerView 
    { 
     public string machineNumber { get; set; } 
     public string machineLocation { get; set; } 
     public string telemetryDevice { get; set; } 
     public string physid { get; set; } 
     public string routeNumber { get; set; } 
     public string driver { get; set; } 
     public DateTime date { get; set; } 
    } 

    public void FileManagerPopulate(IList<FileManagerView> data) 
    { 
     dataGridView1.DataSource = data; 
    } 
+0

你可能會注意到從/到日期時間被混淆了,這是因爲我正在測試我把它們混合起來的可能性:) –

回答

0

所以問題是使用==操作數而不是>和<當GetFiles()方法檢查文件日期是否在選擇範圍內時特德範圍。

我打算在此留下它,因爲它可能會幫助其他人尋找答案。