I have a DGV bound to a 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 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; It also says this in the properties table. It still is visible in the application when run.
If I modify the postitions of the columns in the list I get the following results. No changed other than the order they appear on the columns list.
DCBID(Visible)--DCBModLink(Invisible)--EQModModelNumber(Visible)
DCBModLink(Visble)--DCBID(InVisible)--EQModModelNumber(Visible)
EQModModelNumber(Visible)--EQModModelNumber(Invisible)--DCBID(Invisible)
I have masked the symptom of this issue by leaving the column that I want in the first slot but I dont know why this particular DGV is behaving this way. There is another DGV on the same form that works fine no matter what position the columns are in.
I looked here: First Column Not Hiding in datagridview, and here datagridview id column will not hide But they just moved the problematice columns to the right like I did.
So. My questions are.
Are there other setting that I am not aware of that can override the visibility pararameter?
Has anyone else seen this and how did they stop it?
i use an extension method for this which is my own.. and works very well..also this method sets the dgv client height and width programmatically.. with this ext method just
1-)add dgv to your form
2-)create lists of columns and header texts as what you want to show the user
3-) call the method as yourDGV.showTheGivenColumns(your params, yourparams, yourparams)
P.S. : i changed the names to english and add summary in english..here is the code..
/// <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.");
}
///after this line if your dgv has vertical and/or horizontal scroll, method will check and set the size with this option
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;
}
YOU MAY WANT TO KNOW THESE :
1-) in your form if your dgv's size seems bigger than total columns then please set
d.GetPreferredWidth(DataGridViewAutoSizeColumnMode.AllCells, false);
method's .AllCells
option to your form's needs.. if you set the false
to true
then your dgv will show the columns which flagged as half-showed..so we never need to change this ;)
2-) in checking the needs of scrollbar code part, the numbers which added to width or height are depends to screen resolution..the numbers which i add is not the best but as much optimized for 1024*768, 1280*800 and 1366*968 resolution..You may need to change these added numbers..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With