This question is related to one I asked earlier:
Adding buttons to a TabControl Tab in C#
In short: I have programmatically added controls to tabs in a c# form.
I would now like to access the tab’s controls (in this case a DataGridView) and set some values.
this.dataGridView1.Rows[r].Cells[1].Value = "General";
Above is how I have done it before, but I can’t use this right now due to scope, so I need to access the DataGridView via the parent:
// THIS IS NON WORKING CODE NO COMMENTS ABOUT THE SYNTAX PLEASE
languageTabs.TabPages[0].Controls["grid"].Row[int].Cells[int].Value = "General";
// TabControl -> First Tab -> DataGridView -> the column -> row -> set value
Is there a way to do the same functionality of the first code snippet, but using the parents like in the second snippet?
EDIT:
Here is some more code if it helps:
while ((line = stringReader.ReadLine()) != null)
{
//if the line isn't a comment (comments start with an '/')
if (line[0] != '/')
{
// split the string at each tab
string[] split = line.Split(new char[] { '\t' });
// is this line the "blah"?
if (split[0] == "blah")
{
// we now need to set up the tables to be used
for (int i = 1; i < split.Length; i++)
{
// add a tab for each language
string tabTitle = split[i];
newTab = new TabPage(tabTitle);
newTab.Name = tabTitle;
languageTabs.TabPages.Add(newTab);
// add a DataGridView to each tab
grid = new DataGridView();
grid.SetBounds(14, 68, 964, 420);
grid.Name = tabTitle + "Grid";
// set the columns in the DataGridView
stringIdColumn = new DataGridViewTextBoxColumn();
stringIdColumn.HeaderText = "String ID";
stringIdColumn.Width = 75;
stringIdColumn.Name = tabTitle + "StringIDColumn";
grid.Columns.Insert(0, stringIdColumn);
...
...
// add the DataGridView and button to each tab
languageTabs.TabPages[split[i]].Controls.Add(grid);
}
}
else
{
// this isn't the identifier it must be the start of the languages
// load the strings to the tables
// THIS IS WHERE I WANT MY CODE
}
This is how I got it working: