【问题标题】:Access a DataGridView via a parent (TabControl) to set value -- C# form通过父级 (TabControl) 访问 DataGridView 以设置值 - C# 表单
【发布时间】:2011-11-29 13:09:16
【问题描述】:

这个问题与我之前提出的一个问题有关:

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

有没有办法实现第一个代码 sn-p 的相同功能,但使用第二个 sn-p 中的父母?


编辑:

如果有帮助,这里还有一些代码:

 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
                        }

【问题讨论】:

    标签: c# forms datagridview tabcontrol


    【解决方案1】:

    这就是我的工作方式:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多