我有几个TabPage-s的TabControl。 每个TabPage保留对应的DataGridView对象 依次与相应的DataTable绑定。 每个DataTable对象都是3列的字符串表(名称,值,注释)。 因此,每个TabPage看起来都很好。没关系。 在此绑定期间,我想在每个选项卡中隐藏几行。
DataFridView LoadDataToTab(ref DataTable dt, TabPage tab)
{
DataGridView grid = new DataGridView();
BindingSource source = new BindingSource();
source.DataSource = dt;
grid.DataSource = source;
tab.Control.Add(grid);
// Now all binding is done;
// Now I need to hide some rows in TabPage-s:
HideSomeRows(ref grid);
return grid;
}
void HideSomeRows(ref DataGridView grid)
{
for (int i = 0; i < grid.Rows.Count; i++)
{
string val = grid.Rows[i].Cells[0].Value.ToString();
// SomeString is some "xyz" stirng
if( val == SomeString ||
val == SomeOtherString)
{
grid.Rows[i].Visible = false;
}
}
}
问题:此TabControl中只有“第一个” TabPage会隐藏指定的行。
答案 0 :(得分:0)
与同事一起进行了几次实验后,我们找到了解决方案: 在隐藏DataGridView的行之前先在TabControl中选择当前的TabPage:
...
Tab.SelectedIndex = tabIndex;
...
HideSomeRows(ref grid);
请查看已更改为可以正常使用的同一程序,并由签名
/*!*/
。
DataFridView LoadDataToTab(ref DataTable dt, TabPage tab, TabControl Tab/*!*/, int tabIndex/*!*/)
{
DataGridView grid = new DataGridView();
BindingSource source = new BindingSource();
source.DataSource = dt;
grid.DataSource = source;
tab.Control.Add(grid);
// Now all binding is done;
// This TabControl Tab has inside TabPage tab
Tab.SelectedIndex = tabIndex; /*!*/
// Now I need to hide some rows in TabPage-s:
HideSomeRows(ref grid);
return grid;
}
void HideSomeRows(ref DataGridView grid)
{
for (int i = 0; i < grid.Rows.Count; i++)
{
string val = grid.Rows[i].Cells[0].Value.ToString();
// SomeString is some "xyz" stirng
if( val == SomeString ||
val == SomeOtherString)
{
grid.Rows[i].Visible = false;
}
}
}