通过父级 (TabControl) 访问 DataGridView 来设置值 -- C# 表单
这个问题与我之前问过的一个问题相关:
简而言之:我以编程方式向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我让它工作的方式:
This is how I got it working: