我尝试在所选标签中添加datagridview。我没有遇到任何编译错误,但网格视图没有出现在我的标签页中。
以下是我的代码:
if (tbPerasaan2.Controls[0] == tbPerasaan2.SelectedTab)
{
for (int Tabcount = 0; Tabcount < tbPerasaan2.SelectedIndex; Tabcount++)
{
DataSet ds = new DataSet();
DataTable data = new DataTable();
DataGridView dgJPerasaan = new DataGridView();
//this.tbPerasaan2.SelectedTab
TabPage t = tbPerasaan2.TabPages[0];
tbPerasaan2.SelectedTab = t;
t.Controls.Add(dgJPerasaan);
// this.Controls.Add(dgJPerasaan);
dgJPerasaan.DataSource = data;
}
}
答案 0 :(得分:0)
如果要在tabpage1中创建datagridview,请替换 Tabcount&lt; tabControl1.SelectedIndex 与 Tabcount&lt; = tabControl1.SelectedIndex 因为 if(tbPerasaan2.Controls [0] 是tabcontrol的tabpage1。所以它始终是tabpage1的结果。
if (tbPerasaan2.Controls[0] == tbPerasaan2.SelectedTab) //tbPerasaan2.Controls[0] results tabpage1
{
for (int Tabcount = 0; Tabcount <= tbPerasaan2.SelectedIndex; Tabcount++)
{
DataSet ds = new DataSet();
DataTable data = new DataTable();
DataGridView dgJPerasaan = new DataGridView();
//this.tbPerasaan2.SelectedTab
TabPage t = tbPerasaan2.TabPages[0];
tbPerasaan2.SelectedTab = t;
t.Controls.Add(dgJPerasaan);
// this.Controls.Add(dgJPerasaan);
dgJPerasaan.DataSource = data;
}
}
它将在tabpage1中创建datagridview。