我试图将位置0和1的可见性设置为false

我的代码:

                    mainContenttabControl.TabPages.Add(myTabPage);                     mainContenttabControl.TabPages[i].Controls.Add(new DataGridView()                     {                         Name = "dataGridView_" + tabTitle,                         Dock = DockStyle.Fill,                         DataSource = selectedSensors,                         AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill, //SET HERE COLUMN VISIBLITY = false                     }); 

但是我不能在这里访问属性.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?

有趣的部分是它在那里...

===============>>#1 票数:0

如果您想隐藏Form中几个DGV的前两列,那么我可以使用下面的Linq递归函数返回DGV并隐藏目标列:

private IEnumerable<DataGridView> GetAllDGVs(Control container) {     var controls = container.Controls.Cast<Control>();     return controls.SelectMany(c => GetAllDGVs(c))         .Concat(controls.OfType<DataGridView>()); } 

和用法:

private void hideDatalayerColumns() {     GetAllDGVs(this).ToList().ForEach(dgv =>     {         dgv.Columns[0].Visible = false;         dgv.Columns[1].Visible = false;     }); } 

祝好运。

  ask by cidh translate from so

本文未有回复,本站智能推荐: