DataGridView列显示虽然标记为不可见

我在这里绑定了绑定源生成代码的DGV。

// dgvDocumentList // this.dgvDocumentList.AllowUserToAddRows = false; this.dgvDocumentList.AllowUserToDeleteRows = false; this.dgvDocumentList.AutoGenerateColumns = false; this.dgvDocumentList.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvDocumentList.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.dMTitleDataGridViewTextBoxColumn, this.urlCol, this.idCol}); this.dgvDocumentList.DataSource = this.docListFetchBindingSource; this.dgvDocumentList.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvDocumentList.Location = new System.Drawing.Point(3, 3); this.dgvDocumentList.MultiSelect = false; this.dgvDocumentList.Name = "dgvDocumentList"; this.dgvDocumentList.ReadOnly = true; this.dgvDocumentList.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.dgvDocumentList.Size = new System.Drawing.Size(336, 493); this.dgvDocumentList.TabIndex = 0; this.dgvDocumentList.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvDocumentList_CellContentDoubleClick); this.dgvDocumentList.SelectionChanged += new System.EventHandler(this.dgvDocumentList_SelectionChanged); 

列代码在这里。

  // dCBModLinkDataGridViewTextBoxColumn // this.dCBModLinkDataGridViewTextBoxColumn.DataPropertyName = "DCBModLink"; this.dCBModLinkDataGridViewTextBoxColumn.HeaderText = "DCBModLink"; this.dCBModLinkDataGridViewTextBoxColumn.Name = "dCBModLinkDataGridViewTextBoxColumn"; this.dCBModLinkDataGridViewTextBoxColumn.ReadOnly = true; this.dCBModLinkDataGridViewTextBoxColumn.Visible = false; // // dCBIDDataGridViewTextBoxColumn // this.dCBIDDataGridViewTextBoxColumn.DataPropertyName = "DCBID"; this.dCBIDDataGridViewTextBoxColumn.HeaderText = "DCBID"; this.dCBIDDataGridViewTextBoxColumn.Name = "dCBIDDataGridViewTextBoxColumn"; this.dCBIDDataGridViewTextBoxColumn.ReadOnly = true; this.dCBIDDataGridViewTextBoxColumn.Visible = false; // // eQModModelNumberDataGridViewTextBoxColumn // this.eQModModelNumberDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; this.eQModModelNumberDataGridViewTextBoxColumn.DataPropertyName = "EQModModelNumber"; this.eQModModelNumberDataGridViewTextBoxColumn.HeaderText = "Model Number"; this.eQModModelNumberDataGridViewTextBoxColumn.Name = "eQModModelNumberDataGridViewTextBoxCol 

如您所见,dcbModLinkDataGridViewTextBoxColumn的列定义表示visble = false; 它还在属性表中说明了这一点。 它在运行时仍然可以在应用程序中看到。

如果我修改列表中列的邮件,我会得到以下结果。 除了它们出现在列列表中的顺序之外没有其他更改。

DCBID(可见光) – DCBModLink(隐形) – EQModModelNumber(可见光)

DCBModLink(Visble) – DCBID(不可见) – EQModModelNumber(可见光)

EQModModelNumber(可见光) – EQModModelNumber(隐形) – DCBID(隐形)

我通过在第一个插槽中留下我想要的列来掩盖了这个问题的症状,但我不知道为什么这个特定的DGV会以这种方式运行。 在同一表格上有另一个DGV,无论列位于什么位置都能正常工作。

我看了一下: 第一列没有隐藏在datagridview中 ,这里datagridview id列不会隐藏但是他们只是把问题列移到了右边,就像我一样。

所以。 我的问题是。

  1. 是否有其他我不知道的设置可以覆盖可见性参数?

  2. 有没有人看过这个,他们是如何阻止它的?

我使用了一个扩展方法,这是我自己的..并且工作得很好..此外,此方法以编程方式设置dgv客户端高度和宽度..使用此ext方法只是

1-)将dgv添加到您的表单

2-)创建列和标题文本列表,作为您要向用户显示的内容

3-)将方法称为yourDGV.showTheGivenColumns(your params, yourparams, yourparams)

PS:我将名字改为英文,并在英文中添加摘要..这里有代码..

 ///  /// when this method called, it sets visible = false the columns where not in List of column names /// The column names List count and header List count must be the same number ///  /// DGV which calls this ext method /// the data source load method of the DGV which calls this ext method /// The columnNames List which contains the columns will show.. columnName List's type is List /// The list where you can set the dgv's headers as you prefer..it's type is also List ///  public static DataGridView showTheGivenColumns(this DataGridView dgvName, object dataSourceLoadMethod, List columnNameList, List headerList) { dgvName.DataSource = null; dgvName.Columns.Clear(); dgvName.DataSource = dataSourceLoadMethod; int j = columnNameList.Count; int m = 0; int s = headerList.Count; if (j == s) { foreach (DataGridViewColumn d in dgvName.Columns) { for (int i = 0; i < j; i++) { if (d.Name == columnNameList[i]) { d.HeaderText = headerList[i]; d.Visible = true; d.Width = d.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false); m += d.Width; break; } else { d.Visible = false; } } } } else { MessageBox.Show("Count of Header and ColumnName Lists are not equal..Please Check."); } 

///在此行之后,如果您的dgv具有垂直和/或水平滚动,则方法将检查并使用此选项设置大小

 bool vscroll = (dgvName.DisplayedRowCount(false) < dgvName.Rows.Count); bool hscroll = (dgvName.DisplayedColumnCount(false) < columnNameList.Count); if (vscroll == true) { int vScrollWidth = (dgvName.Controls.OfType().First()).Width; dgvName.Width = m + (vScrollWidth + 5); } if ( vscroll == false) { dgvName.Width = m + 5; } if (hscroll == true) { int hscrollWidth = (dgvName.Controls.OfType().First()).Height; dgvName.Height = ((dgvName.RowTemplate.Height * dgvName.RowCount) + dgvName.ColumnHeadersHeight) + hscrollWidth; } if (hscroll == false) { dgvName.Height = ((dgvName.RowTemplate.Height * dgvName.RowCount) + dgvName.ColumnHeadersHeight) + 2; } dgvName.ClearSelection(); dgvName.ReadOnly = true; return dgvName; } 

你可能想知道这些:

1-)在你的表格中,如果你的dgv的大小似乎比总列数大,那么请设置

 d.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false); 

方法的.AllCells选项满足您表单的需要..如果您将false设置为true那么您的dgv将显示标记为half-shows的列。所以我们永远不需要更改它;)

2-)在检查滚动条代码部分的需求时,添加到宽度或高度的数字取决于屏幕分辨率。我添加的数字不是最好的,但是对1024 * 768,1280 * 800和1366进行了优化* 968分辨率..您可能需要更改这些添加的数字..