0

I have four dgv's on a Winform. I successfully hid the unneeded columns on the first 3 dvg's by setting their "Visible" property to false. However, there is one column that will not hide on the fourth dgv and I cannot figure out why.

Here is the code for the fourth dgv:

With Me.DataGridView4
    .DataSource = BSSJ
    .AutoGenerateColumns = True
    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
    .Columns("RecID").Visible = False
    .Columns("JobID").Visible = False
    .Columns("Deleted").Visible = False
    .Columns("Amt").DefaultCellStyle.Format = "#,##0.00"
    .Columns("Amt").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
End With

It is the "RecID" column that still appears in the dgv. The other two columns, "JobID" and "Deleted", are hidden as expected.
The data source, "BSSJ", is a bindingsource that is bound to an SQL table. I cannot omit this field from the Select statement of the query because it is needed in the relationship with the parent table.
The "RecID" is a primary key in the table bound to "BSSJ", but the other three dgv's have their primary key hidden successfully.
I'm probably missing something simple, but I just do not see it.
Any ideas?

djv
  • 15,168
  • 7
  • 48
  • 72
  • Have you tried using the ordinal instead of the name? E.g., `Columns(0).Visible = False` instead of `Columns("RecID").Visible = False`? Maybe it's just a typo. – Jimi Nov 27 '19 at 20:54
  • Yes, I tried that and the column still shows. – Larry Stinson Nov 29 '19 at 14:47
  • Try issuing a .Columns.Clear first as you are Autogenerating as it's possible the cols you are adding are in addition to existing columns and the column you've hidden is correct but way off to the right of the grid. Just a guess, but I've done it. Or just try removing the column all together as you can access it's contents directly from the datasource or bindingsource if needed. – Jon Roberts Nov 29 '19 at 17:12

1 Answers1

0

This is not a direct answer as to why your code is not working, I could not replicate your issue.

But if you remove your autosizecolumns line of code, then you can set the width of the columns dynamically and potentially work around the issue

.Columns("RecID").Width = 0
.Columns("Amt").Width = 200
.Columns("JobID").Visible = False
.Columns("Deleted").Visible = False
.Columns("Amt").DefaultCellStyle.Format = "#,##0.00"
.Columns("Amt").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
Mr.B
  • 1
  • 6