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());
}
}