The DataGridView column shows, although invisible

I have a DGV related to the binding source generation code here.

            // 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);

The column code is listed here.

  // 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

As you can see, the column definition for dcbModLinkDataGridViewTextBoxColumn says visble = false; He also talks about this in the property table. It is still displayed in the application at startup.

If I change the entries of the columns in the list, I get the following results. No change except the order they appear in the column list.

DCBID (visible) - DCBModLink (invisible) - EQModModelNumber (visible)

DCBModLink (Visble) - DCBID (Invisible) - EQModModelNumber (Visible)

EQModModelNumber (Visible) - EQModModelNumber (Invisible) - DCBID (Invisible)

, , , , DGV . DGV , , .

: datagridview, datagridview id , .

. .

  • , , ?

  • - ?

+5
1

, .. . dgv.. ext

1-) dgv

2-) ,

3-) yourDGV.showTheGivenColumns(your params, yourparams, yourparams)

P.S.: .. ..

/// <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 type is List<string></param>
  /// <param name="Header">The list where you can set the dgv headers as you prefer..it 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.");
                }

/// , 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;
             }

:

1-) , dgv , , ,

d.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false);

.AllCells .. false true, dgv , ... ;)

2-) , , , . , , , 1024 * 768, 1280 * 800 1366 * 968. , .

+1

All Articles