我正在尝试将位置0和1的可见性设置为false 在我的构造函数中:
public void InitializeData()
{
setCompanyList(dataLayer.Companys);
setTruckList(dataLayer.GetTrucks(GetActiveCompanyId()));
UpdateTabPages();
}
我的代码:
private void UpdateTabPages()
{
mainContenttabControl.TabPages.Clear();
int selectedTruck = GetSelectedTruckId();
var selectedHardwares = dataLayer.GetHardware(selectedTruck);
var selectedSensors = dataLayer.GetSensors(selectedTruck);
var selectedService = dataLayer.GetService(selectedTruck);
var selectedRepairs = dataLayer.GetRepair(selectedTruck);
List<string> tabMenuTitles = new List<string>();
tabMenuTitles.Add("Hardware");
tabMenuTitles.Add("Sensors");
tabMenuTitles.Add("Service");
tabMenuTitles.Add("Repair");
int i = -1;
foreach (var tabTitle in tabMenuTitles)
{
i++;
TabPage myTabPage = new TabPage(tabTitle);
switch (tabTitle)
{
case "Hardware":
mainContenttabControl.TabPages.Add(myTabPage);
mainContenttabControl.TabPages[i].Controls.Add(new DataGridView()
{
Name = "dataGridView_" + tabTitle,
Dock = DockStyle.Fill,
AutoGenerateColumns = true,
DataSource = selectedHardwares,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
});
break;
case "Sensors":
mainContenttabControl.TabPages.Add(myTabPage);
mainContenttabControl.TabPages[i].Controls.Add(new DataGridView()
{
Name = "dataGridView_" + tabTitle,
Dock = DockStyle.Fill,
AutoGenerateColumns = true,
DataSource = selectedSensors,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
});
break;
case "Service":
mainContenttabControl.TabPages.Add(myTabPage);
mainContenttabControl.TabPages[i].Controls.Add(new DataGridView()
{
Name = "dataGridView_" + tabTitle,
Dock = DockStyle.Fill,
AutoGenerateColumns = true,
DataSource = selectedService,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
});
break;
case "Repair":
mainContenttabControl.TabPages.Add(myTabPage);
mainContenttabControl.TabPages[i].Controls.Add(new DataGridView()
{
Name = "dataGridView_" + tabTitle,
Dock = DockStyle.Fill,
AutoGenerateColumns = true,
DataSource = selectedRepairs,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
});
break;
default:
Console.WriteLine("You sir, you Failed!");
break;
}
}
CreateFileOpenColumns();
hideDatalayerColumns();
}
但是我无法在此处访问.Column属性,我还尝试编写一个函数,该函数在我的“ setDataGridViews()”函数之后被调用,但仅在表单已加载时才起作用
private void hideDatalayerColumns()
{
int dataGridViewHardwareIndex = mainContenttabControl.TabPages[0].Controls.IndexOfKey("dataGridView_" + "Hardware");
int dataGridViewSensorsIndex = mainContenttabControl.TabPages[1].Controls.IndexOfKey("dataGridView_" + "Sensors");
int dataGridViewServiceIndex = mainContenttabControl.TabPages[2].Controls.IndexOfKey("dataGridView_" + "Service");
int dataGridViewRepairIndex = mainContenttabControl.TabPages[3].Controls.IndexOfKey("dataGridView_" + "Repair");
if (dataGridViewHardwareIndex >= 0)
{
DataGridView myTabGridViewHardware = mainContenttabControl.TabPages[0].Controls[dataGridViewHardwareIndex] as DataGridView;
myTabGridViewHardware.Columns[0].Visible = false;
myTabGridViewHardware.Columns[1].Visible = false;
}
if (dataGridViewSensorsIndex >= 0)
{
DataGridView myTabGridViewSensors = mainContenttabControl.TabPages[1].Controls[dataGridViewSensorsIndex] as DataGridView;
myTabGridViewSensors.Columns[0].Visible = false;
myTabGridViewSensors.Columns[1].Visible = false;
}
if (dataGridViewServiceIndex >= 0)
{
DataGridView myTabGridViewService = mainContenttabControl.TabPages[2].Controls[dataGridViewServiceIndex] as DataGridView;
myTabGridViewService.Columns[0].Visible = false;
myTabGridViewService.Columns[1].Visible = false;
}
if (dataGridViewRepairIndex >= 0)
{
DataGridView myTabGridViewRepair = mainContenttabControl.TabPages[3].Controls[dataGridViewRepairIndex] as DataGridView;
myTabGridViewRepair.Columns[0].Visible = false;
myTabGridViewRepair.Columns[1].Visible = false;
}
}
是否存在一种变通办法或更好的解决方案,即我的两列为.Visible = false?