简体   繁体   English

DataGridView ID 列不会隐藏

[英]DataGridView ID Column Will Not Hide

I have a DataGridView bound to an ObjectDataSource some of the columns are hidden including the ID column.我有一个绑定到 ObjectDataSource 的 DataGridView,其中一些列是隐藏的,包括 ID 列。 The problem is that the ID column shows up even when its visible property is set to false.问题是,即使 ID 列的可见属性设置为 false,它也会显示出来。 Has anyone run into this problem before?有没有人遇到过这个问题? Setting the width to zero is not an option since the grid doesn't allow columns with a width less than 5 pixels wide so it still shows the column on the grid no matter what.将宽度设置为零不是一个选项,因为网格不允许宽度小于 5 像素的列,因此无论如何它仍然在网格上显示该列。

The strange thing is that the ID column wasn't always showing.奇怪的是 ID 列并不总是显示。 After I worked on the app for a bit the columns appeared again.我在应用程序上工作了一段时间后,这些列又出现了。

DataGridView is not set to auto generate columns. DataGridView 未设置为自动生成列。 I am building to version 4.0 of .NET and C#.我正在构建 .NET 和 C# 的 4.0 版。

Here is the code in the form constructor.这是表单构造函数中的代码。

dgvActiveMiners.AutoGenerateColumns = false;
dgvAvilableMiners.AutoGenerateColumns = false;
dgvOperationResults.AutoGenerateColumns = false;

dgvActiveMiners.Columns["dgvActiveMinersRecordId"].Visible = false;
dgvAvilableMiners.Columns["dgvAvilableMinersRecordId"].Visible = false;
dgvOperationResults.Columns["dgvOperationResultRecordId"].Visible = false;

This is the generated code for the grids.这是为网格生成的代码。

this.dgvOperationResults.AllowUserToAddRows = false;
this.dgvOperationResults.AllowUserToDeleteRows = false;
this.dgvOperationResults.AutoGenerateColumns = false;
this.dgvOperationResults.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvOperationResults.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dgvOperationResultRecordId,
this.nameDataGridViewTextBoxColumn2,
this.typeIdDataGridViewTextBoxColumn,
this.amountDataGridViewTextBoxColumn,
this.operationIdDataGridViewTextBoxColumn});
this.dgvOperationResults.DataSource = this.operationResultBindingSource;
this.dgvOperationResults.Location = new System.Drawing.Point(12, 40);
this.dgvOperationResults.MultiSelect = false;
this.dgvOperationResults.Name = "dgvOperationResults";
this.dgvOperationResults.ReadOnly = true;
this.dgvOperationResults.Size = new System.Drawing.Size(498, 247);
this.dgvOperationResults.TabIndex = 16;

I don't know what else I could be missing?我不知道我还能缺少什么?

Thanks!谢谢!

Suggestion 1:建议1:
Try explicitly setting the DGV Column's Visible property to false in the FormLoad event:尝试在 FormLoad 事件中将 DGV 列的 Visible 属性显式设置为 false:

dataGridView.Columns["YourIdColumn"].Visible = false;

Suggestion 2:建议2:
Try changing your column dgvActiveMinersRecordId from the first column in the DGV to the last column.尝试将 dgvActiveMinersRecordId 列从 DGV 中的第一列更改为最后一列。

To try and answer this a bit more generically for the next person who comes along, like me...尝试为下一个来的人更一般地回答这个问题,比如我......

This does seem to be a bug, but the work around is to:这似乎是一个错误,但解决方法是:

Make sure the columns you want to hide are displayed last确保您要隐藏的列最后显示

This will depend on your code, but for some this will be:这将取决于您的代码,但对于某些代码,这将是:

  • SQL code changed to return the columns later SQL 代码更改为稍后返回列
  • Change the code that adds the datagridview columns putting the "to hide" columns at the end更改添加 datagridview 列的代码,将“隐藏”列放在最后
  • Setting the Columns[x].DisplayIndex such that the columns appear last, as per @Steve's post根据Columns[x].DisplayIndex的帖子,设置Columns[x].DisplayIndex使列最后出现

I have the same issue.我有同样的问题。

The following line, still leaves the column visible, even though exploring the value shows it false.以下行仍然使该列可见,即使探索该值显示它为 false。

dataSelected.Columns["id"].Visible = false;

I didn't have this issue, until I had set the DisplayIndex on a column我没有这个问题,直到我在列上设置了 DisplayIndex

dataSelected.Columns["ipagenum"].DisplayIndex = 6;

Moving the offending columns DisplayIndex to the end, corrected this issue.将有问题的列 DisplayIndex 移到最后,更正了这个问题。

dataSelected.Columns["id"].DisplayIndex = 15;

Maybe a bit late but I was beating myself up with the same problem, I had two separate forms with DataGridViews both bound to different DataTables.也许有点晚了,但我遇到了同样的问题,我有两个单独的表单,DataGridViews 都绑定到不同的 DataTables。 One had no problem hiding the 1st column, on the other everything I tried didn't work, until ...一个人隐藏第一列没有问题,另一方面我尝试过的一切都不起作用,直到......

Note: ["newCol"] is the first (ie column 0) column in the data table.注意:["newCol"] 是数据表中的第一列(即第0列)。

This code Fails to hide column [0] (or by name ["NewRow"])此代码无法隐藏列 [0](或按名称 ["NewRow"])

...
MyDataGridView.DataSource = MyDatatable;
MyDataGridView.Columns["NewRow"].Visible = false;   // doesn't hide (col 0)
// MyDataGridView.Columns[0].Visible = false;  <<<< this didn't work either
MyDataGridView.Columns["Changed"].Visible = false;
MyDataGridView.Columns["Active"].Visible = false;
MyDatatable.RowFilter = "[Active] = 1";
...

this code works :此代码有效

...
MyDataGridView.DataSource = MyDatatable;
MyDatatable.RowFilter = "[Active] = 1";
MyDataGridView.Columns["NewRow"].Visible = false;   // YAY!! Now it hides
// MyDataGridView.Columns[0].Visible = false;       <<<< and this works too
MyDataGridView.Columns["Changed"].Visible = false;
MyDataGridView.Columns["Active"].Visible = false;
...

Spot the difference?指出不同? It's Where I specify the RowFilter.这是我指定 RowFilter 的地方。

The other form doesn't have a starting RowFilter but in both forms I later change the RowFilter (depending on user actions), column 0 never comes back.另一个表单没有起始行过滤器,但在这两种表单中,我后来更改了行过滤器(取决于用户操作),第 0 列永远不会回来。

Seems specifying RowFilter too soon after hiding columns fails for column 0.在隐藏列 0 的列失败后,似乎过早地指定了 RowFilter。

Very very weird!!!!非常非常奇怪!!!! Very very frustrating!!!!非常非常郁闷!!!!

I would like to contribute a perspective that has not been mentioned.我想贡献一个尚未提及的观点。

Each column of the DataGridView has a property name. DataGridView 的每一列都有一个属性名称。 You can directly access the column by name, as you would to access any other element.您可以按名称直接访问该列,就像访问任何其他元素一样。 For example: ColumnName.Property = AnyProperty.例如:ColumnName.Property = AnyProperty。 In your case: ColumnName.Visible = false.在您的情况下:ColumnName.Visible = false。

I think it is cleaner, more direct, and less likely to make a mistake.我认为它更简洁、更直接,而且不太可能出错。 We also help a bit the compiler :)我们也对编译器有所帮助:)

In this way, it is not necessary to use the property name of the DataGridView, neither locate the desired column mediate a string (which potentially can be commited an error).这样,既不需要使用 DataGridView 的属性名称,也不需要定位所需的列来调解字符串(这可能会导致错误)。 I mean this: YourDataGridView.Columns ["YourColumn"] Property = AnyProperty.我的意思是:YourDataGridView.Columns ["YourColumn"] Property = AnyProperty。

That's odd.这很奇怪。

Are you certain you are calling the right column name?您确定您调用的是正确的列名吗? I realize that would be a stupid mistake to make, but it happens!我意识到这将是一个愚蠢的错误,但它发生了!

Here's a simple test you could try:这是您可以尝试的简单测试:

void test(string columnName, bool visibility) {
  if (dataGridView1.Columns.Contains(columnName)) {
    dataGridView1.Columns[columnName].Visible = visibility;
  } else {
    throw new Exception(string.Format("Column '{0}' does not exist in DataGridView '{1}'.", columnName, dataGridView1.Name));
  }
}

I had the same issue and none of the above worked for me.我遇到了同样的问题,以上都不适合我。 My fix was to set a DataPropertyName at least for the column that should be hidden in the designer at "Edit columns".我的解决方法是至少为应该隐藏在设计器中的“编辑列”中的列设置一个 DataPropertyName。

If you want to hide a column by name, you have to give a Name at your column.如果要按名称隐藏列,则必须在列中指定名称。 Initiliaze the property Name and after you can use it by code.初始化属性名称,然后您可以通过代码使用它。

I was having the same problem, and I did not want have to change the index of my id column to the visible property works.我遇到了同样的问题,我不想将我的 id 列的索引更改为可见属性作品。 So I noticed that after I indicated that the id column visible = false , I was deleting the last row of the DataGridView and this is what was making the id column appear.所以我注意到,在我指出 id column visible = false ,我删除了DataGridView的最后一行,这就是使 id 列出现的原因。 So I delete the row first and then indicate that the id column = false .所以我先删除该行,然后指出 id column = false

I have a similar problem.我有一个类似的问题。 I added an unbound checkbox column to my data bound grid.我在数据绑定网格中添加了一个未绑定的复选框列。 It became the first column.它成为第一列。 When I stepped through my grid like so:当我像这样跨过我的网格时:

for (int i = 0; i < grvQuoteCostSheets.RowCount; i++)
{
   grvQuoteCostSheets[grcQCostSProfit.Index, i].Value
      = (Convert.ToInt32(grvQuoteCostSheets[grcQCostSPrice.Index, i].Value) - Convert.ToInt32(grvQuoteCostSheets[grcQCostSTotalCost.Index, i].Value));
}
idColumn.Visible = false;   //Need to rehide.

The first hidden column visibility switched to False.第一个隐藏列可见性切换为 False。 I moved the CKBox column from first to 3rd place as suggested in a different answer (the next two columns being an ID value & Line# respectively, and hidden) and the checkbox column stayed hidden.我按照不同答案中的建议将 CKBox 列从第一位移到了第三位(接下来的两列分别是 ID 值和 Line#,并且隐藏)并且复选框列保持隐藏状态。 I was hiding the ID column after the math as it was flipping to Visible.我在数学运算后隐藏了 ID 列,因为它正在翻转为 Visible。 I just lived with it before (hiding the ID column after the math loop), but now that I added the checkbox column, I decided to dig a bit, and here I am.我之前只是使用它(在数学循环之后隐藏 ID 列),但是现在我添加了复选框列,我决定深入挖掘一下,现在我来了。 I also tried doing the math in a list grabbed from the grid, and the checkbox column would still flip to Visible when the CKBox was first.我还尝试在从网格中抓取的列表中进行数学运算,当 CKBox 为第一个时,复选框列仍会翻转为可见。 I'm still stuck with the first hidden column wanting to flip to visible, so I re-hide it.我仍然坚持第一个想要翻转为可见的隐藏列,所以我重新隐藏了它。 I do have AutoGenerateColumns = False and am explicitly setting the first column visibility to false as well.我确实有 AutoGenerateColumns = False 并且我也明确地将第一列可见性设置为 false。 Both to no avail.两者都无济于事。

Don't know if anybody is still struggling with the issue.不知道有没有人还在纠结这个问题。 I had the same problem and was setting the visible property in the dataSourceChanged event.我遇到了同样的问题,正在 dataSourceChanged 事件中设置可见属性。

When I put the visible false in the show event instead it worked.当我将可见的 false 放在 show 事件中时,它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM