Best way to pass data to a DataGridView form from another non-form class

纵饮孤独 提交于 2019-12-25 08:47:50

问题


As the title says, I need to pass a list of hashtables from a regular class to a form class to be rendered in a DataGridView. What I've got so far is this:

    namespace somenamespace
{
    class aldeloUpdater
    {
        private static string client = "chanchitos";
        private static string establishment = "c1";

        static void Main()
        {
            try
            {
                var guiForm = new GuiForm(); //  Instantiating the Form-derived class.
                string deliveriesListResp = getOrders();

                Processing...
                foreach (...)
                {
                    if ((bool)DBresponse["status"])
                    {
                        guiForm.dataGridViewProducts = (List<Hashtable>)DBresponse["deliveriesSaved"]; // Passing the data to the DataGridView.

                        foreach (Hashtable delivery in (List<Hashtable>)DBresponse["deliveriesSaved"])
                        {
                            string updateDeliveryResponse = updatePedidoInDomicilios(delivery["domiciliosOrderId"].ToString(), 2, DBresponse["errmsg"].ToString()); 
                        }
                    }
                    else
                    {
                        Processing...
                    }
                }
                guiForm.ShowDialog(); // Showing the form.
                More processing...
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception details: " + e.ToString());
                Console.ReadLine();
            }
        }
    More methods...
}

Now the Form class looks like this:

namespace somenamespace
{
    public partial class GuiForm : Form
    {
        public List<Hashtable> dataGridViewProducts; // Variable used to store the data to be rendered by the DataGridView.

        public GuiForm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void GuiForm_Load(object sender, EventArgs e)
        {
            int index = 0;
            foreach (Hashtable product in dataGridViewProducts)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = product["productName"];
                dataGridView1.Rows[index].Cells[1].Value = product["userName"];
                dataGridView1.Rows[index].Cells[2].Value = product["dateAndTime"];
                dataGridView1.Rows[index].Cells[3].Value = product["domiciliosOrderId"];
                index++;
            } 
        }

        Some more methods.

    }
}

For now this code works just fine and the data is shown in the DataGridView, nonetheless I feel like there must be a better way to achieve this goal, it's just that I'm new to C#. I will appreciate suggestions and even more a code sketch of how you would do this in a better way.

Thanks.

来源:https://stackoverflow.com/questions/38986799/best-way-to-pass-data-to-a-datagridview-form-from-another-non-form-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!