jeudi 16 juin 2016

how to pass a list of objects from one form to another and then bind the list data to a datagridview

I have built a list on form1. I'm trying to pass that list to form2 where I would like to take the data from the list and have it display in form2. I've got it to where the datagridview on form2 will show the column headers but it doesn't show any of the data. I'm suspecting that I'm not passing my list correctly because when I put a simple message box in form2 to display the contents of one of the fields within the first object in the list it gave me an out of index error. Here is form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ProviderPayProject1
{
    public class EDIFile
    {
        public string fullInFilePath { get; set; }
        public string fullOutFilePath { get; set; }
        public string InfileName { get; set; }
        public string OutfileName { get; set; }
        public string UniqueID { get; set; }
        public DateTime infileDateTime { get; set; }
        public DateTime outfileDateTime { get; set; }
        public TimeSpan timeDiff { get; set; }
    }

    public partial class ProviderPayProject1 : Form
    {
        List<EDIFile> FilesInfo = new List<EDIFile>();
        List<EDIFile> InFilesInfo = new List<EDIFile>();
        List<EDIFile> OutFilesInfo = new List<EDIFile>();

        string strSideDoneFirst = "";
        string strSideDoneSecond = "";
        public ProviderPayProject1()
        {
            InitializeComponent();
        }

        private void ProviderPayProject1_Load(object sender, EventArgs e)
        {

        }

        private void btnInbound_Click(object sender, EventArgs e)
        {
            updateListBox(txtBoxInbound, lstBoxInFiles, "Inbound");            
        }

        private void btnOutbound_Click(object sender, EventArgs e)
        {
            updateListBox(txtBoxOutbound, lstBoxOutFiles, "Outbound");
        }

        private void updateListBox(TextBox txtDirectionBox, ListBox lstFilesBox, string strDirection)
        {   
            lstFilesBox.Items.Clear();
            FolderBrowserDialog fbdFileList = new FolderBrowserDialog();
            if (strDirection == "Inbound")
            {
                //fbdFileList.SelectedPath = @"S:340BSafewayAS2INin_archive";
                fbdFileList.SelectedPath = @"C:testingin";
            }
            else if (strDirection == "Outbound")
            {
                //fbdFileList.SelectedPath = @"S:340BSafewayAS2OUTout_archive";
                fbdFileList.SelectedPath = @"C:testingout";
            }

            if (fbdFileList.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtDirectionBox.Text = fbdFileList.SelectedPath;
                string[] strFiles = Directory.GetFiles(fbdFileList.SelectedPath);
                if (strFiles.Count() == 0)
                {
                    lstFilesBox.Items.Add("No Files To Select In Directory");
                }
                else
                {
                    int intFileLength = 0;
                    int counter = 0;
                    lstFilesBox.Items.Clear();

                    foreach (string strFileName in strFiles)
                    {
                        string strShortFileName;
                        strShortFileName = Path.GetFileName(strFileName);
                        if (strShortFileName.Length > intFileLength)
                        {
                            intFileLength = strShortFileName.Length;
                        }
                        counter++;
                    }

                    counter = 0;
                    foreach (string strFileName2 in strFiles)
                    {
                        string strShortFName = Path.GetFileName(strFileName2);
                        String strDirectionID = strShortFName.Substring(0, 6);
                        string strDivStore = strShortFName.Substring(8, 7);                        
                        DateTime strDateTime = File.GetCreationTime(strFiles[counter]);

                        if (strShortFName.Length == intFileLength)
                        {
                            lstFilesBox.Items.Add(strShortFName + "  |  " + strDateTime);
                        }
                        else
                        {
                            lstFilesBox.Items.Add(strShortFName.PadRight(intFileLength + 6) + "  |  " + strDateTime);
                        }

                        if (strDirectionID == "MCD850")
                        {
                            if (counter == 0 && strSideDoneFirst != "outbound")
                            {
                                strSideDoneFirst = "inbound";
                            }
                            else if (strSideDoneFirst != "inbound")
                            {
                                strSideDoneSecond = "inbound";
                            }

                            InFilesInfo.Add(new EDIFile() {fullInFilePath = strFileName2, fullOutFilePath = "No Matching Out-File", InfileName = strShortFName,
                                                OutfileName = "No Matching Out-File", UniqueID = strDivStore, infileDateTime = strDateTime});
                        }
                        else if (strDirectionID == "MCD855")
                        {
                            if (counter == 0 && strSideDoneFirst != "inbound")
                            {
                                strSideDoneFirst = "outbound";
                            }
                            else if (strSideDoneFirst != "outbound")
                            {
                                strSideDoneSecond = "outbound";
                            }
                            OutFilesInfo.Add(new EDIFile() {fullOutFilePath = strFileName2, fullInFilePath = "No Matching In-File", OutfileName = strShortFName,
                                                InfileName = "No Matching In-File", UniqueID = strDivStore, outfileDateTime = strDateTime});
                        }

                        counter++;
                    }
                    if (strDirection == "Inbound")
                    {
                        InboundFileTotal.Text = "In Files Count: " + (InFilesInfo.Count + 1);
                        InboundFileTotal.Show();
                    }
                    else if (strDirection == "Outbound")
                    {
                        OutBoundFileTotal.Text = "Out Files Count: " + OutFilesInfo.Count;
                        OutBoundFileTotal.Show();
                    }
                    if (strSideDoneFirst != "" && strSideDoneSecond != "")
                    {
                        counter=0;
                        foreach (EDIFile fileIn in InFilesInfo)
                        {
                            foreach (EDIFile fileOut in OutFilesInfo)
                            {
                                if (fileIn.UniqueID == fileOut.UniqueID)
                                {
                                    FilesInfo.Add(new EDIFile() {fullOutFilePath = fileOut.fullOutFilePath, fullInFilePath = fileIn.fullInFilePath, OutfileName = fileOut.OutfileName,
                                                  InfileName = fileIn.InfileName, UniqueID = fileIn.UniqueID, outfileDateTime = fileOut.outfileDateTime, infileDateTime = fileIn.infileDateTime});
                                }
                            }
                            counter++;
                        }
                        counter = 0;
                        foreach (EDIFile TimeDiff in FilesInfo)
                        {
                            FilesInfo[counter].timeDiff = TimeDiff.outfileDateTime - TimeDiff.infileDateTime;
                            counter++;
                        }
                        Form2 form2 = new Form2(FilesInfo);
                        form2.ShowDialog();
                    }
                }
            }
        }

        private void lstBoxInFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (object row in lstBoxInFiles.SelectedItems)
            {
                sb.Append(row.ToString());
                sb.AppendLine();
            }
            sb.Remove(sb.Length - 1, 1); // Just to avoid copying last empty row
            Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());
        }

        private void lstBoxOutFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (object row in lstBoxInFiles.SelectedItems)
            {
                sb.Append(row.ToString());
                sb.AppendLine();
            }
            sb.Remove(sb.Length - 1, 1); // Just to avoid copying last empty row
            Clipboard.SetData(System.Windows.Forms.DataFormats.Text, sb.ToString());
        }



        private void btnGenerateReport_Click(object sender, EventArgs e)
        {

        }
    }
}

and here is form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProviderPayProject1
{
    public partial class Form2 : Form
    {
        public Form2(List<EDIFile> EDIFiles2)
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = true;
            Controls.Add(dataGridView1);
            dataGridView1.DataSource = EDIFiles2;            
        }        
    }
}

someone please help me, thanks in advance!

Aucun commentaire:

Enregistrer un commentaire