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

Kas*_*bba 5 c# winforms

我在这里绑定了绑定源生成代码的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);
Run Code Online (Sandbox Code Playgroud)

列代码在这里.

  // 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

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

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

所以.我的问题是.

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

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

sih*_*zzz 1

我为此使用了我自己的扩展方法..并且效果很好..而且此方法以编程方式设置 dgv 客户端高度和宽度..使用此 ext 方法

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

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

3-) 将该方法调用为yourDGV.showTheGivenColumns(your params, yourparams, yourparams)

PS:我将名称更改为英文并添加英文摘要..这是代码..

/// <summary>
  /// 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
  /// </summary>
  /// <param name="dgvName">DGV which calls this ext method</param>
  /// <param name="Method">the data source load method of the DGV which calls this ext method</param>
  /// <param name="ColumnName">The columnNames List which contains the columns will show.. columnName List's type is List<string></param>
  /// <param name="Header">The list where you can set the dgv's headers as you prefer..it's type is also List<string></param>
  /// <returns></returns>
  public static DataGridView showTheGivenColumns(this DataGridView dgvName, object dataSourceLoadMethod, List<string> columnNameList, List<string> 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.");
                }
Run Code Online (Sandbox Code Playgroud)

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

bool vscroll = (dgvName.DisplayedRowCount(false) < dgvName.Rows.Count);
                bool hscroll = (dgvName.DisplayedColumnCount(false) < columnNameList.Count);
                if (vscroll == true)
                {
                    int vScrollWidth = (dgvName.Controls.OfType<VScrollBar>().First()).Width;
                    dgvName.Width = m + (vScrollWidth + 5);
                }
                if ( vscroll == false)
                {
                    dgvName.Width = m + 5;
                }
                if (hscroll == true)
                {
                    int hscrollWidth = (dgvName.Controls.OfType<HScrollBar>().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;
             }
Run Code Online (Sandbox Code Playgroud)

您可能想了解这些:

1-) 在您的表单中,如果您的 dgv 的大小似乎大于总列数,请设置

d.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false);
Run Code Online (Sandbox Code Playgroud)

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

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