Question

Im currently creating a Windows Form Application where I want to get data from a database that will be synchronized to as many target databases that I can find in a specific table.

To do so, I made a TabControl inside my Form 1 with TabPages created pragmatically for every target db. For each TabPage I create a DataGridview to store the data and I let the user select the records they want to synchronize.

foreach (DataRow row in targetDatabases.Rows)
{
    //Checking for target databases that aren't Master
    if (int.Parse(row["IS_MASTER"].ToString()) == 0)
    {
        //Creating a new TabPage for every target found in the Table
        TabPage newPage = new TabPage(row["CODE"].ToString());

        // I'm not sure if this should be
        newPage.Parent = tabControl;
        // or tabControl.Controls.Add(newPage)

        //Creating a new DataGridView for every target found
        DataGridView newDgv = new DataGridView();

        //Also not sure if this should be
        newDgv.Parent = newPage;
        //or newPage.Controls.Add(newDgv)

        //Setting the DataSource
        currDGV.DataSource = Model.getGridViewData(xmlpath, 0);

        //Layout settings for the DataGridView
        .
        .
        .
        }
    }
    // Return the TabControl to the View
    return tabControl;

No problem untill now.

Now the user can select which records he wants to be synchronized and to which target.

This is what it looks like:

enter image description here

But now, I don't know how to access the data in each DataGridView to save the users selection to a file or to start the synchronization.

I tried something like this:

for (int i = 0; i < targetDatabaseCount; i++)
{
    tabPages[targetDatabaseCount] = (TabPage)inputTabControl.GetControl(targetDatabaseCount);
    foreach (TabPage tabPage in tabPages)
    {
       DataGridView tempDgv =  //don't know how to get the gridview from the TabPage
    }
}

Please be lenient with me as I am an apprentice and this is my first post here :)

Was it helpful?

Solution

You could iterate the controls in each tab page and if the control is datagridview you could cast it and use.

Like this:

foreach (TabPage tbp in tbctrl.TabPages)
        {
            foreach (Control ctrl in tbp.Controls)
            {
                if (ctrl is DataGridView)
                {
                    DataGridView newDgv = (DataGridView)ctrl;
                    string strValue = newDgv.Rows[0].Cells[0].Value.ToString();
                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top