我有一个绑定到DataSource的DataGridView,它根据用户输入将其列隐藏在DataBindingComplete事件中。 它工作正常,除非需要隐藏所有列,始终显示最后一个要隐藏的列。 在调试期间,我可以看到该列设置为visible = false但它没有效果。
我有一个正常的解决方法,在DataBindingComplete事件中触发一个计时器并调用HideColumns()方法,但我正在寻找更好的解决方案。
private void CoverageDGV_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
var datagridview = sender as DataGridView;
datagridview.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
datagridview.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
datagridview.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
datagridview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
datagridview.RowHeadersVisible = true;
datagridview.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
datagridview.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
datagridview.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
HideColumns(datagridview);
//timer1.Enabled = true; // This works fine
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
HideColumns(ShipCoverageDGV);
}
private void HideColumns(DataGridView datagridview)
{
for (int i = 0; i < datagridview.Columns.Count; i++)
{
var column = datagridview.Columns[i];
var itemType = [USER INPUT];
if (itemType == Glossary.IndirectCOType)
column.Visible = IndirectCOCheckBox.Checked;
else if (itemType == Glossary.NotAllocatedType)
column.Visible = NotAllocatedCheckBox.Checked;
else
column.Visible = DirectCOCheckBox.Checked;
}
}
我的问题是:如何隐藏所有列?
答案 0 :(得分:-3)
dataGridView1.Columns[0].Visible = false;