Cannot Hide Column in a DataGridView - sql

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?

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

Related

Unbound ComboBox populates another combobox

How do I get a combobox to populate depending on what value you chose in another combobox? The first combobox values come from a table. The second combobox could either be a list or a date. Working on list part right now. If the value will give a list, the unbound combobox is visible and enabled. That works - I just can't get the second combobox to populate - should be values from another "code" table. Tried when exiting the first combobox OR when entering the second combobox. Neither works (used same code). Help!
Private Sub Combo746_Exit(Cancel As Integer)
Dim strsql As String
If (Me!Combo746.Value = "Category") Then
Me.Combo750.Enabled = True
Me.Combo750.visible = True
Me.Text748.Enabled = False
Me.Text748.visible = False
Me.Combo750.SetFocus
strsql = "Select [Category] FROM [dbo_CodesCategory]"
Me!Combo750.RowSource = strsql
Else
End If
End Sub
I got this to work. First combobox does not populate second combobox, but instead goes to the correct combobox with values. To explain - Department values are in the Department combobox - combodept. Category values are in a Category combobox - combocat. First combobox has values Category and Department. If the user picks Department, combodept for Department will be visible, combobox for Category- combocat - will be invisible. Here is the code:
If (Me!Combo746.Value = "Category") Then`
Me.ComboCat.Enabled = True
Me.ComboCat.visible = True
Me.ComboDept.Enabled = False
Me.ComboDept.visible = False
Me.ComboCat.SetFocus
Else
If (Me!Combo746.Value = "Department") Then
Me.ComboDept.Enabled = True
Me.ComboDept.visible = True
Me.ComboCat.Enabled = False
Me.ComboCat.visible = False
Me.ComboDept.SetFocus
End If
End If

DataGridView WrapMode + AutoSizeColumnsMode

How do I wrap my DataGridView's headers (long texts without break line) at the same time have it's AutoSizeColumnsMode to Fill?
For i As Integer = 0 To DataGridView1.Columns.Count - 1
DataGridView1.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
Next
I have this code to wrap the text, but will ignore my DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill (which leaves my DataGridView with empty gray spaces)
You are setting .AutoSizeMode property for every column in your DataGridView; to resolve your problem you can set .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill on one column (i.e. the last visible column).
You must also set .ColumnHeadersDefaultCellStyle.WrapMode property to specify that a textual content in a DataGridView cell is wrapped to subsequent lines or truncated when it is too long to fit on a single line.
Code example:
YourDataGridView.ColumnHeadersDefaultCellStyle.WrapMode = False
For i As Integer = 0 To YourDataGridView.Columns.Count - 1
If i = YourDataGridView.Columns.Count - 2 Then
YourDataGridView.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
Else
YourDataGridView.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
End If
Next i
Please note that I use YourDataGridView.Columns.Count - 2 because my last column is not visible so I need to apply this property to the previous column.

How to hide columns based on name in Excel 2010?

I am trying to hide columns based on name using VBA inside Excel 2010. Each of my columns have a product version and some results below it. The product version does repeat throughout the spreadsheet since I have it categorized by OS. Thus, I'm hiding multiple columns based on selection, like a filter would do. If I could hide based on the name and not the column letter (A,B,C,...), then adding columns in between in the future would prevent more code changes on the location of those columns.
What I'm currently doing right now is fixed to the column letter. This limits me in the sense that I cannot add columns in between without having to change the code (column letter). Example:
`If productver_2dot5.Value = True Then
Columns("E").Hidden = False
Columns("M").Hidden = False
Columns("AC").Hidden = False
Columns("AT").Hidden = False
Columns("BD").Hidden = False
Columns("BR").Hidden = False
Else
Columns("E").Hidden = True
Columns("M").Hidden = True
Columns("AC").Hidden = True
Columns("AT").Hidden = True
Columns("BD").Hidden = True
Columns("BR").Hidden = True
End If`
What I would like to do is to hide any columns that contains the name 'Product Ver 2" (example) in one of its cells.
Sub HideBlahs()
Dim col As Range
For Each col In ActiveSheet.UsedRange.Columns
If Application.CountIf(col, "blah") > 0 Then
col.EntireColumn.Hidden = True
End If
Next col
End Sub
FYI your posted code reduces to:
Range("E1,M1,AC1,AT1,BD1,BR1").EntireColumn.Hidden = Not productver_2dot5.Value

datagridview and empty table

DataGridView1.DataSource = ds.Tables("Orders")
If the query returns no results (empty dataset table) I see -1 in first column of the DataGridView in OrderID column. When I click on a grid columns header it decreases -2, -3 etc.
How to fix it?
How about:
If ds.Tables("Orders").Rows.Count = 0 Then
lblNoResults.Visible = True
DataGridView1.Visible = False
Else
lblNoResults.Visible = False
DataGridView1.DataSource = ds.Tables("Orders")
'Anything else you need to do
DataGridView1.Visible = True
End If
lblNoResults would be a label with text something like "No results found" that you would display instead of your DataGridView.
Basically, don't bind the datasource if there are no rows in it.
It might be that there are bugs in your other code causing your specific problem. If you want to show more of your code then it'll be easier to say what's going wrong.

DataGridView column order does not seem to work

I have a DataGridView bound to a list of business objects:
Dim template As New IncidentTemplate
Dim temps As List(Of IncidentTemplate) = template.LoadAll
Dim bs As New BindingSource
If Not temps Is Nothing Then
bs.DataSource = temps
Me.dgvTemplates.DataSource = bs
End If
I then add an unbound button column and make some of the bound columns invisible:
Dim BtnCol As New DataGridViewButtonColumn
With BtnCol
.Name = "Edit"
.Text = "Edit"
.HeaderText = String.Empty
.ToolTipText = "Edit this Template"
.UseColumnTextForButtonValue = True
End With
.Columns.Add(BtnCol)
BtnCol = Nothing
For Each col As DataGridViewColumn In Me.dgvTemplates.Columns
Select Case col.Name
Case "Name"
col.DisplayIndex = 0
col.FillWeight = 100
col.Visible = True
Case "Description"
col.DisplayIndex = 1
col.FillWeight = 100
col.Visible = True
Case "Created"
col.DisplayIndex = 3
col.HeaderText = "Created On"
col.DefaultCellStyle.Format = "d"
col.FillWeight = 75
col.Visible = True
Case "CreatedBy"
col.DisplayIndex = 2
col.HeaderText = "Created By"
col.FillWeight = 75
col.Visible = True
Case "Edit"
col.DisplayIndex = 4
col.HeaderText = ""
col.FillWeight = 30
col.Visible = True
Case Else
col.Visible = False
End Select
Next
This seems to work reasonably well except no matter what I do the button column (Edit) always displays in the middle of the other columns instead of at the end. I've tried both DGV.Columns.Add and DGV.Columns.Insert as well as setting the DisplayIndex of the column (this works for all other columns) but I am unable to make the button column display in the correct location. I've also tried adding the button column both before and after setting the rest of the columns but this seems to make no difference Can anyone suggest what I am doing wrong? It's driving me crazy....
I stumbled on your post while looking to solve a similar problem. I finally tracked it down by doing as you mention (using the DisplayIndex property) and setting the AutoGenerateColumns property of the DataGridView to false. This property is not visible in the designer, so I just added it to the constructor for my form. Be sure to do this before setting the data source for your grid.
Hope this helps...
The AutoCreateColumn is the problem. The following article gives an example for how to fix it.
From DataDridView DisplayOrder Not Working
When setting the column’s DisplayIndex in a data grid view, some columns were out of order. To fix the issue, I had to set AutoGenerateColumns to true before setting the data source, and to FALSE after.
For Example:
dgvReservedStalls.AutoGenerateColumns = True
dgvReservedStalls.DataSource = clsReservedStalls.LoadDataTable()
dgvReservedStalls.AutoGenerateColumns = False
Same problem here, and after searching for a while, I found out that by setting the DisplayIndexes in Ascending order made it for me.
It's counter-intuitive, because it's a number, but I still had to set them in order.
This works:
With customersDataGridView
.Columns("ContactName").DisplayIndex = 0
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
.Columns("Country").DisplayIndex = 3
.Columns("CompanyName").DisplayIndex = 4
End With
While this did not:
With customersDataGridView
.Columns("ContactName").DisplayIndex = 0
.Columns("CompanyName").DisplayIndex = 4
.Columns("Country").DisplayIndex = 3
.Columns("ContactTitle").DisplayIndex = 1
.Columns("City").DisplayIndex = 2
End With
I tried the above solutions without success. Then I found this article: It made it all better.
http://msdn.microsoft.com/en-us/library/vstudio/wkfe535h(v=vs.100).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1