I am returning SQL results into a DataGridView and have run into a problem. I have an option on my form to only show checked values but can't get it working. Here's my code:
For x As Integer = dgvAutogrow.Rows.Count - 1 To dgvAutogrow.Rows.Count
If dgvAutogrow.Rows(x).Cells("checked").Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Here's what part of my DataGridView looks like. I want an event to occur that shows only values that have the check box checked.
When I debug I get the following error:
Any suggestions on what to change?
You have to cast the cell as DataGridViewCheckBoxCell and then test the value like this:
For x As Integer = dgvAutogrow.Rows.Count - 1 To 0 Step -1
Dim cel as DataGridViewCheckBoxCell
cel = CType(dgvAutogrow.Rows(x).Cells("YourColumnName"), DataGridViewCheckBoxCell)
If cel.Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Also, you had an ArgumentException because the column you specified doesn´t exist, you need to change that.
Code is tried and tested.
You can try this. We need to cast the cell as a DataGridViewCheckBoxCelland change your loop...
For x As Integer = dgvAutogrow.Rows.Count - 1 To 0 Step -1
If CType(dgvAutogrow.Rows(x).Cells("checked"), DataGridViewCheckBoxCell).Value = False Then
dgvAutogrow.Rows.Remove(dgvAutogrow.Rows(x))
End If
Next
Related
The first column in a DataGridView (dgv1) has checkboxes. Interestingly, when you click on the last checkbox desired, as soon a you loop through the rows for that column to evaluate checked items, the last cell checked (with a cursor on it) does not evaluate to true. The syntax for looping through the checkboxes in column 0 is below:
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next
Is there a way to set the focus to false if the cell containing the last checkbox checked is in edit mode?
BTW, refreshing the edit status (below) did not help before looping:
dgv1.RefreshEdit()
Per #JohnG's comment, the suggestion worked as follows:
dgv1.EndEdit()
Dim NumCBChecked As Integer = 0
For i = 0 To dgv1.Rows.Count - 1
If CBool(dgv1(0, i).Value) = True Then
NumCBChecked += 1
End If
Next
When I run code below, the selected row returned is always from the bottom up. How can I get it to loop from the top of the Data Grid table each time?
'Find the selected customer by code. Display closest match in grid.
Dim targetString As String = txtAccountCode.Text
For Each row As DataGridViewRow In frmCustomerLookUp.GSCUSTDataGridView.Rows
If row.Cells(0).Value.ToString().StartsWith(targetString) Then
frmCustomerLookUp.GSCUSTDataGridView.ClearSelection()
frmCustomerLookUp.GSCUSTDataGridView.Rows(row.Index).Selected = True
frmCustomerLookUp.GSCUSTDataGridView.FirstDisplayedScrollingRowIndex = frmCustomerLookUp.GSCUSTDataGridView.SelectedRows(0).Index
Dim selectedIndex = frmCustomerLookUp.GSCUSTDataGridView.SelectedRows(0).Index
frmCustomerLookUp.GSCUSTDataGridView.Rows(selectedIndex).Selected = True
frmCustomerLookUp.GSCUSTDataGridView.Rows(selectedIndex).Cells(0).Selected = True
Exit Sub
End If
Next
Use a For...Next loop, starting at the last row and stepping -1
For index As Integer = frmCustomerLookUp.GSCUSTDataGridView.Rows.Count - 1 To 0 Step -1 ' Or Count -2
If frmCustomerLookUp.GSCUSTDataGridView.Rows(index).Cells(0).Value.ToString().StartsWith(targetString) Then
As the other person said. (Just a bit shorter)
For x=frmCustomerLookUp.GSCUSTDataGridView.Rows.Count - 1 To 0 Step -1
if frmCustomerLookUp.GSCUSTDataGridView(0,x).ToString().StartsWith(targetString) then
'something
end if
next
I'm having the code takes the input from my checkboxes and grab data from the related worksheet. I ran it line by line and found out that it always gets a runtime error at the If statement on the final loop. Is there something wrong in my code?
Option Explicit
Private Sub UserForm_Initialize()
Dim counter As Long
Dim chkBox As MSForms.CheckBox
''''Add checkboxes based on total sheet count
For counter = 1 To Sheets.count - 2
Set chkBox = Me.Frame1.Controls.Add("Forms.CheckBox.1", "CheckBox" & counter)
chkBox.Caption = Sheets(counter + 2).Name
chkBox.Left = 10
chkBox.Top = 5 + ((counter - 1) * 20)
Next
End Sub
Private Sub cmdContinue_Click()
Dim Series As Object
Dim counter As Long
'''Clear old series
For Each Series In Sheets(2).SeriesCollection
Sheets(2).SeriesCollection(1).Delete
Next
''Cycle through checkboxes
For counter = 1 To Sheets.count - 2
''If the box is checked then
If Me.Frame1.Controls(counter).Value = True Then ''Error here on 4th iteration
''Add new series
With Sheets(2).SeriesCollection.NewSeries
.Name = Sheets(counter + 2).Range("$A$1")
.XValues = Sheets(counter + 2).Range("$A$12:$A$25")
.Values = Sheets(counter + 2).Range("$B$12:$B$25")
End With
End If
Next counter
Me.Hide
End Sub
Also, a second problem is it always run on the wrong loop. If i check box 2 it'll run data for the box 1 sheet, 3 run for 2, 4 run for 3, and 1 run for 4. Can anyone explain the reason behind this?
EDIT: So as VincentG point out below, adding an explicit name "checkbox" in there did the trick (i didn't know you could do that). Index 1 was probably taken by one of the buttons or the frame in the user form, causing it to get off set.
I guess your main problem comes from the fact that the controls have to be accessed starting from index 0. So to loop over all controls, you would do something like
For counter = 0 To Me.Frame1.Controls.Count - 1
Debug.Print counter; Me.Frame1.Controls(counter).Name
Next counter
So, when you stick to you code, I assume you have to change the if-statement to
If Me.Frame1.Controls(counter-1).Value = True Then
I am hoping to use the string value of a selected Listbox item in a .Match function within VBA - I need the the value '1' to be entered into the row where the value of the selection matches a value in column "A:A", on a specific column.
What I thought I would be able to do is to use a .value argument for the selected ListBox item, however this seems to either error out or give me a Boolean response, which isn't what I am after (I am after the actual string value of the item).
I have already looped through all items to set the Selected argument to True, and then I am looping through the list one by one to add '1' to the correct range.
Here is the code I thought would work (but doesn't, it throws an error of "Run-time error '13': Type mismatch" which is presumably down to the .Value not being a String.
For x = 0 To Me.CreditsEmployeesListBox.ListCount - 1
Me.CreditsEmployeesListBox.Selected(x) = True
Next
For i = 0 To Me.CreditsEmployeesListBox.ListCount - 1
If Me.CreditsEmployeesListBox.Selected(i) = True Then
employeeRow = WorksheetFunction.Match(Me.CreditsEmployeesListBox(i).Value, IndexSheet.Range("A:A"), 0)
IndexSheet.Range(Cells(employeeRow, showCodeColumn).Address).Value = 1
End If
Next
It errors out on the 'employeeRow = ...' line. Here, I am essentially trying to ask it:
employeeRow = WorksheetFunction.Match(<value of the currently referenced ListBox item>,IndexSheet.Range("A:A"),0)
Is this possible with VBA or am I going about this the wrong way?
Thanks
Matt
As an "hybrid" answer (as there is more than one problem) try this:
For x = 0 To Me.CreditsEmployeesListBox.ListCount - 1
Me.CreditsEmployeesListBox.Selected(x) = True
Next
Dim employeeRow As Variant
For i = 0 To Me.CreditsEmployeesListBox.ListCount - 1
If Me.CreditsEmployeesListBox.Selected(i) = True Then
employeeRow = Application.Match(Me.CreditsEmployeesListBox.List(i), IndexSheet.Columns(1), 0)
If IsNumeric(employeeRow) Then IndexSheet.Cells(employeeRow, showCodeColumn).Value = 1
End If
Next
This also should avoid VBA-errors.
If any questions are left, just ask :)
Hello Everyone Good Afternoon
I have a program that imports excel into datagridview but when after I import it the look is like this.
Link of the Image
as what you see in the image above I have a Column Total which composed of NULL(Empty String),0 and a certain data.
I wonder how can I delete the whole row if the column Total has a Data of NULL or 0.
Im using this code and it applies in Datagridview
Private Sub step_3_delete_Zero()
Dim SkipRemove As Boolean
Dim Rowindex As Integer
For Rowindex = DataGridView1.Rows.Count - 1 To 0 Step -1
SkipRemove = False
For Each Cell As DataGridViewCell In DataGridView1.Rows(Rowindex).Cells
If Not Cell.Value.ToString = "0" Then
SkipRemove = True
Exit For
End If
Next
If Not SkipRemove = False Then
DataGridView1.Rows.RemoveAt(Rowindex)
End If
Next
End Sub
I hope someone helps me, TY in advance
In your for each loop where have you said to look in the total cell?
Are you looking in every cell in that row?
Also isn't If SkipRemove = True Then easier to read than If Not SkipRemove = False Then?
I think it's not working because you are looping through and checking each cell rather than looping through by row and then checking the specific cell on each row.
Would something along the lines of this be more easier?
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("Total").Value = "0" Then
DataGridView1.Rows.Remove(row)
End If
Next