1
votes

I am looking for some solution how to get the data from dynamically made datagridview in dynamically created tabpage in tabcontrol. After I made gridviews then I populate it from Excel files. I do not know how to call the specific datagridview in other method if I haven't created it permanently in my project.

Below is my method's code how I generate dynamically tab pages and fill dynamically created datagridviews in TabControl. Any help will be really appreciate :-)

private void FillPages()
{
    try
    {
        for (int i = 0; i <= listView.Items.Count - 1; i++)
        {
            string path = listView.Items[i].Text.ToString();
            Variables.fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
            Variables.FullPath = listView.Items[i].Text.ToString();
            string valueMonth = comboBoxMonth.Text.ToString();
            string valueYear = comboBoxYear.Text.ToString();

            string excelQuery = "select * from [AAB$]";

            try
            {
                    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                                              + path + ";Extended Properties=\"Excel 12.0;HDR=YES;\";";
                    OleDbConnection con = new OleDbConnection(connectionString);
                    OleDbCommand cmdOLEDB = new OleDbCommand(excelQuery, con);
                    con.Open();

                    OleDbDataAdapter da = new OleDbDataAdapter(cmdOLEDB);
                    DataTable data = new DataTable();
                    da.Fill(data);

                    Variables.fileName = Path.GetFileName(path);
                    TabPage tab = new TabPage();
                    tab.Text = Variables.fileNameWithoutExtension;
                    DataGridView grid = new DataGridView();
                    grid.Dock = DockStyle.Fill;
                    grid.AllowUserToAddRows = false;

                    grid.DataSource = data;
                    grid.AutoResizeColumns();
                    grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                    tab.Controls.Add(grid);
                    tabControlForDataUploaded.Controls.Add(tab);
                    labelTabName.Text = tabControlForDataUploaded.TabPages[0].Text.ToString();
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
1
make DataGridView grid as global.sowjanya attaluri
just declare the DataGridView out of the method FillPages.Hugo S. Mendes
@sowjanyaattaluri Aren't there many DataGridViews to get data from? If so, then one global object reference won't do the trick. I'd only like to ask, where is the problem with using the data? You have your own datatable, binding it to the source and then it's bound to the datagridview. Do you want global access to those datatables also outside that loop or are you asking for a basic way to acess the data from the datatabe?D. Petrov
global I mean to say is out of the scope of the FillPages.sowjanya attaluri
I have many datagridviews. It depends on how many files will be uploaded (see list view loop for)MROCZEK

1 Answers

1
votes

A bound DataGridView is a display mechanism for its underlying DataSource. Rather than "get data from a grid", in most cases, you should be concerned instead with getting data from the underlying sources. In your code above, you are dynamically creating those sources, adding them as the DataSource to a dynamically created grid, and then leaving the method with the only way to access both the grids and their sources being drilling into a controls collection of the tab and casting.

What you need is a more direct way to store references to and access your datasources. I would define a property on your form that holds a collection of DataTables, and add your datatables to this collection before setting them as a datasource for your grids. This way you can later get to any or all of your DataTables simple by accessing that collection.

        public Collection<DataTable> Tables { get; set; }
        private void FillPages()
        {
            for (int i = 0; i <= listView.Items.Count - 1; i++)
            {
                // omitted for clarity
                DataTable data = new DataTable();
                grid.DataSource = data;
                Tables.Add(data);
            }
        }
    }

and then later

    private void AccessData()
    {
        foreach(var table in Tables)
        {
            MessageBox.Show(table.Rows[0]["SomeColumn"].ToString());

        }
    }