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

发布于 2024-12-18 16:43:34 字数 2843 浏览 0 评论 0原文

这个问题与我之前问过的一个问题相关:

将按钮添加到C# 中的 TabControl Tab

简而言之:我以编程方式向 ac# 形式的选项卡添加了控件。

我现在想访问选项卡的控件(在本例中为 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
                        }

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
                        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆流 2024-12-25 16:43:34

这就是我让它工作的方式:

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

This is how I got it working:

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;
                            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文