Gridview Show and Hide a Specific Column - vb.net

I have a gridview in which a specific column Date. I have set the Visible property of column to false because I want to show on different conditions of page. Please tell me how can I do it using vb.net that my Date column should show or hide at runtime
Update
My current code is
If Not Page.User.Identity.Name = "bilal" Then
GridView1.AutoGenerateEditButton = False
GridView2.AutoGenerateEditButton = False
GridView3.AutoGenerateEditButton = False
Else
GridView1.AutoGenerateEditButton = True
GridView1.AutoGenerateColumns = True
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the first '
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(0).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(0).Visible = True
Next
End If
GridView2.AutoGenerateEditButton = True
GridView3.AutoGenerateEditButton = True
End If

If you've set AutoGenerateColumns to True, the Column-Count will be 0, then you need to loop the rows and show/hide the appropriate cells. Otherwise you can use the Visible property.
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the 4.'
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(3).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(3).Visible = True
Next
End If

Related

How can I remove the cell selection from a checkbox column ? VB.NET

Greetings to the community.
I am trying to modify a checkbox column of a DataGridView in VB.NET. I hope you can help me.
I am looking to go from this:
To this:
That is, even if I click or double click on the cell, the checkbox is not selected.
Part of the code where the columns are added to DataGridView (DvgNeumaticos):
If BtnVistaPorFacturar.Checked Then
For i As Integer = 0 To DgvNeumaticos.Columns.Count - 1
DgvNeumaticos.Columns(i).Visible = False
Next
DgvNeumaticos.Columns("ColChk").Visible = True
DgvNeumaticos.Columns("CodOS").Visible = True
DgvNeumaticos.Columns("Tipo").Visible = True
DgvNeumaticos.Columns("NroOrden").Visible = True
DgvNeumaticos.Columns("NroOrden").DisplayIndex = 0
DgvNeumaticos.Columns("FechaOS").Visible = True
DgvNeumaticos.Columns("F.Cierre").Visible = True
DgvNeumaticos.Columns("Observacion").Visible = True
DgvNeumaticos.Columns("CodNeumatico").Visible = True
DgvNeumaticos.Columns("Externo").Visible = True
DgvNeumaticos.Columns("CodTercero").Visible = True
DgvNeumaticos.Columns("CodProducto").Visible = True
DgvNeumaticos.Columns("Producto").Visible = True
DgvNeumaticos.Columns("Trabajo").Visible = True
DgvNeumaticos.Columns("NroDocRecepcion").Visible = True
DgvNeumaticos.Columns("FechaRecepcion").Visible = True
New Edit:
If BtnVistaPorFacturar.Checked Then
Dim chkCol As New DataGridViewCheckBoxColumn
chkCol.Name = "ColChk"
chkCol.HeaderText = "Chk"
DgvNeumaticos.Columns.Add(chkCol)
dt = WFOrdServicioNeumaticos.Instancia.fdu_NEUM_ORDSERV_VerOrdenesPorFacturar(.Cells("IDAgente").Value)
End If
Thanks for the replies.

What is lacking in my VBA code? Looking to have multiple checkboxes that when one is selected, it hides all other rows

Brand new to coding junk in VBA for Microsoft Word. I have a table with 12 rows and I want to place a standard content control checkbox next to each row, and when any given checkbox is checked, the other rows disappear.
Currently I've had good luck at this with purely text, but trying to bookmark to hide an entire row of a table only seems to work for the very first checkbox. (Sorry if my code is more complicated than it needs to be. I also skipped pasting all of the code since the other 10 lines are the same, so the final 12 End Ifs are necessary):
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Title = "impact" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = False
End If
Exit Sub
Else: If cc.Title = "license" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = False
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next
End Sub
Assuming that the content control Title is the same as the bookmark name you can try this simplified version of your code.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If ActiveDocument.Bookmarks.Exists(cc.Title) Then
ActiveDocument.Bookmarks(cc.Title).Range.Font.Hidden = cc.Checked
End If
Next cc
End Sub
EDIT:
The issue you have with your original code is that it will only allow one row to be hidden.
To make your solution work you need to query the checked status of the corresponding content control for each bookmark. Your best option to achieve that is to ensure that the bookmark name matches either cc.Title or cc.Tag, otherwise you are back to complex and unwieldy code.
You actually don't need anything more complicated than:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
If .Range.Information(wdWithInTable) = True Then
If .Checked = True Then
.Range.Tables(1).Range.Font.Hidden = True
.Range.Rows(1).Range.Font.Hidden = False
Else
.Range.Tables(1).Range.Font.Hidden = False
End If
End If
End With
End Sub
Looping through all the content controls is quite unnecessary. You don't even need any titles or bookmarks.

Filter ListView data with CheckBox inputs

I'm trying to provide a way to filter data within a ListView using CheckBox controls. Checking a box will add or remove the selected attribute from the ListView.
The current code:
For i = 0 To RecCount
Filter = True
'RecordDat array populated here
'filter the record by checkbox selection
If ckbComplete.Checked = True And Len(RecordDat(7)) = 0 Then
Filter = False
ElseIf ckbOpen.Checked = True And Len(RecordDat(7)) > 0 Then
Filter = false
ElseIf ckbApps.Checked = True And strings.Left(RecordDat(0).ToString, 4) <> "APPS" Then
Filter = False
ElseIf ckbContract.Checked = True And Strings.Left(RecordDat(0), 3) <> "SOP" Then
Filter = false
End If
If Filter = False Then GoTo SkipItem
' More code adding the item to the list view and formatting
SkipItem:
Next
At the moment I can filter only once per array index, i.e. I can filter ckbComplete and ckbApps but not ckbcomplete, ckbApps and ckbContract.
I managed to figure out a solution by trial and error and wish to share what I found.
In order to filter the data, each variable (in this case, the column value) required its own filter statements, each which must be encased in an If ELSE statement.
The resulting code was as follows:
Dim Filter As Boolean = True
For i = 0 To RecCount
Filter = False
'RecordDat initialisation
Filter = FilterPass(RecordDat)
If Filter = False Then Continue For
Dim itm As ListViewItem
itm = New ListViewItem(RecordDat)
Healthchart.ListView1.Items.Add(itm)
'Continue formatting and input of data into listview
Next
The FilterPass functions then follow:
Private Function FilterPass(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbComplete.Checked = True Then
If Len(RecordDat(7)) > 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
If Healthchart.ckbOpen.Checked = True Then
If Len(RecordDat(7)) = 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
Return filter
End Function
Private Function FilterPass2(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbApps.Checked = True Then
If Strings.Left(RecordDat(0).ToString, 4) = "APPS" Then
filter = True
End If
End If
If Healthchart.ckbContract.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "SOP" Then
filter = True
End If
End If
If Healthchart.ckbTech.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "ATS" Then
filter = True
End If
End If
If Healthchart.ckbDes.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "DES" Then
filter = True
End If
End If
Return filter
End Function
Private Function FilterPass3(ByVal RecordDat() As String)
Dim filter As Boolean = True
Dim SubDate As Date = Date.ParseExact(RecordDat(1).ToString, "dd/MM/yy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
If Healthchart.ckbLast30.Checked = True Then
If DateAdd(DateInterval.Day, -30, Now) > SubDate Then
filter = False
End If
End If
If Healthchart.ckb7Days.Checked = True Then
If DateAdd(DateInterval.Day, -7, Now) > SubDate Then
filter = False
End If
End If
Return filter
End Function
One can then add as many 'filters' as required by adding an additional If Filter = true then into the FilterPass function.

Assign value to checkboxes

Hye there, I new with vba here.
I want to use checkboxes to link with the series collections of a chart. I put the check boxes in a sheet which contain the chart altogether. I have a lot of checkboxes here to be assigned to "true" value.
Private Sub Controls_Initialize()
'Make default for checkboxes
CheckBox1.Value = True
CheckBox2.Value = True
CheckBox3.Value = True
CheckBox4.Value = True
CheckBox5.Value = True
CheckBox6.Value = True
CheckBox7.Value = True
CheckBox8.Value = True
CheckBox9.Value = True
CheckBox10.Value = True
CheckBox11.Value = True
CheckBox12.Value = True
CheckBox13.Value = True
CheckBox14.Value = True
CheckBox15.Value = True
CheckBox16.Value = True
CheckBox17.Value = True
CheckBox18.Value = True
CheckBox19.Value = True
CheckBox20.Value = True
CheckBox21.Value = True
CheckBox22.Value = True
CheckBox23.Value = True
CheckBox24.Value = True
End Sub
I have tried this code but can't
For i = 1 to 24
Controls("CheckBox" & i).Value = True
Next i
The questions are
1. Is there any other code that can make it simple?
2. How to link the check boxes with the series collection in the activechart? Example, if the checkbox return value false, the series collection will be deleted/hide(perhaps?). And when it returns value true, the series collection of the same data will be added back in the chart. I would like to make the chart interactive.
If there is any reference that I can reviewed, do tell me.
Thanks in advance.
Regards.
Alright, so assuming from what you've given, I'd think the problem is that the interpreter doesn't know i is an integer.
To fix this, we can implement something along the lines of Dim i As Integer to implement i as an integer.
We could try this:
Dim i As Integer
For i = 1 to 24
Controls("CheckBox" & i).Value = True
Next i

Setting ReadOnly attribute to all Textboxes in Array of Controls

I have the following code looping through a variety of arrayed controls in a form:
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
If ctrlArray(r).ReadOnly = False Then
ctrlArray(r).ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next
I receive the error "'ReadOnly' is not a member of System.Windows.Forms.Control" when trying to set textboxes as read only.
Solved this right before I hit the submit button. Thought I would share anyway:
Dim tbx As TextBox
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
tbx = ctrlArray(r)
If tbx.ReadOnly = False Then
tbx.ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next