通过父级(TabControl)访问DataGridView以设置值 - C#表单

时间:2011-11-29 13:09:17

标签: c# forms datagridview tabcontrol

这个问题与我之前提到过的问题有关:

Adding buttons to a TabControl Tab in C#

简而言之:我已经以编程方式将控件添加到c#表单中的选项卡。

我现在想要访问选项卡的控件(在本例中为DataGridView)并设置一些值。

this.dataGridView1.Rows[r].Cells[1].Value = "General";

上面是我以前做过的,但由于范围我现在无法使用它,所以我需要通过父进程访问DataGridView:

// 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

有没有办法对第一个代码段执行相同的功能,但是使用第二个代码段中的父项?


编辑:

如果它有帮助,可以使用以下代码:

 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
                        }

1 个答案:

答案 0 :(得分:0)

这就是我的工作方式:

Control[] ctrls = languageTabs.TabPages[1].Controls.Find("EnglishGrid", true);
                            if (ctrls[0] != null)
                            {
                                DataGridView dgv = ctrls[0] as DataGridView;
                                dgv.Rows[r].Cells[0].Value = 1;
                            }