C#DataGridView没有从单独的表单

时间:2017-10-08 01:13:00

标签: c# winforms datagridview

对不起,如果我的头衔很糟糕,凌晨1点30分,我全都没咖啡了。我已尝试过针对类似问题提出的一些解决方案,但我无法弄清楚,因为我的案例有点不同。

我尝试使用单独的表单来为我用于格式化数据的方法选择参数,这些数据以原始形式传递给DataGridView,但是它没有填充DataGridView。我已经将新表单设置为一个对话框,它在调用时接收原始表单引用,在表单上有一个DateTimePicker和一个按钮,当单击该按钮时,它调用一个获取datetime值的方法,然后调用原始表单上的方法,将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;
    }

1 个答案:

答案 0 :(得分:0)

所以问题是使用==操作数而不是&gt;和&lt;当GetFiles()方法检查文件日期是否在选定范围内时。

我要离开这里,因为这可能有助于其他人寻找答案。