簡體   English   中英

C#DataGridView沒有從單獨的表單填充

[英]C# DataGridView not populating from a separate form

很抱歉,如果我的頭銜不好,是凌晨1點30分,我已經沒有咖啡了。 我已經嘗試過針對類似問題提出的一些解決方案,但是由於我的情況有些不同,因此我無法弄清楚。

我正在嘗試使用一種單獨的形式來選擇用於格式化數據的方法的參數,該數據以原始形式傳遞給DataGridView,但是它沒有填充DataGridView。 我將新表單設置為對話框,在調用它時會收到原始表單引用,在表單上有一個DateTimePicker和一個按鈕,單擊該按鈕時它將調用一個獲取datetime值的方法,然后調用在原始表單上傳遞日期時間參數的方法,然后關閉對話框。 原始表單上的方法以傳遞參數的方式運行,以獲取DataGridView的數據,然后調用將綁定列表傳遞給它的datasource方法。

這種使用對話框填充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;
    }

因此,問題在於當GetFiles()方法檢查文件日期是否在所選范圍內時,使用==操作數而不是>和<。

我將其保留在此處,因為它可能會幫助其他人尋找答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM