How To Update A Janus GridEx Checkbox At Runtime - vb.net

I have a Janus GridEx control named grdLOB that can have multiple rows. Each row has a checkbox and 3 additional columns. If a checkbox is unchecked on a certain row (GL) I need to loop thru the grid and uncheck the checkboxes in the other rows.
Here is the code I have, but obviously it's not working...
Private Sub grdLOB_CellValueChanged(ByVal sender As Object, ByVal e as Janus.Windows.GridEX.ColumnActionEventArgs) Handles grdLOB.CellValueChanged
If e.Column.Key = cSelector Then
Dim grd As Janus.Windows.GridEX.GridEX = CType(sender, GridEX)
Dim row As GridEXRow = grd.GetRow(grd.Row)
Select Case row.Cells(1).Value.ToString().ToUpper()
Case "GL"
'If GL is checked, do nothing to the other rows.
'If GL is unchecked, uncheck all the other rows.
If CBool(row.Cells(0).Value) = False Then
For Each gr As GridEXRow In grdLOB.GetRows()
gr.BeginEdit()
gr.Cells(0).Value = False
gr.EndEdit()
Next
End If
Case Else
'If a row other than GL is unchecked, do nothing to the other rows.
'If a row other than GL is checked, then check the GL row.
If CBool(row.Cells(0).Value) = True Then
For Each gr As GridEXRow In grdLOB.GetRows()
If gr.Cells(1).Value = "GL" Then
gr.BeginEdit()
gr.Cells(0).Value = True
gr.EndEdit()
End If
Next
End If
End Select
End If
End Sub
How can I dynamically check\uncheck checkboxes in the GridEx?

Figured it out. Here's the changed Select Case code that works:
Select Case row.Cells(1).Value.ToString().ToUpper()
Case "GL"
'If GL is checked, do nothing to the other rows.
'If GL is unchecked, uncheck all the other rows.
If CBool(row.Cells(0).Value) = False Then
For i As Integer = 0 To grdLOB.RowCount - 1
Dim gr As GridEXRow = grdLOB.GetRow(i)
gr.IsChecked = False
Next
End If
Case Else
'If a row other than GL is unchecked, do nothing to the other rows.
'If a row other than GL is checked, then check the GL row.
If CBool(row.Cells(0).Value) = True Then
For i As Integer = 0 To grdLOB.RowCount - 1
Dim gr As GridEXRow = grdLOB.GetRow(i)
If gr.Cells(1).Value = "GL" Then
gr.IsChecked = True
End If
Next
End If
End Select

Related

DataGridView live search data

I am trying to add a search function to a DataGridView in vb.net using this code
For i As Integer = 0 To ContactsList.RowCount - 1
For j As Integer = 0 To ContactsList.ColumnCount - 1
If ContactsList.Rows(i).Cells(j).Value.ToString.ToLower.Trim = ContactsListSearch.Text.ToLower.Trim Then
MsgBox("Item found " + i.ToString)
ContactsList.Rows(i).Visible = True
Else
ContactsList.Rows(i).Visible = False
End If
Next
Next
I'm seeing the MsgBox show when the value matches, but the rows are not showing, it just hides all rows
Another possible option is to load data into a DataTable, create a BindingSource, set the BindingSource DataSource property to the DataTable. Set the DataGridView DataSource property to the BindingSource.
The following example works against a column in the DataTable. If the user clears the TextBox the filter is removed while if there is text filter with trim.
Private Sub SearchButton_Click(sender As Object, e As EventArgs) _
Handles SearchButton.Click
If String.IsNullOrWhiteSpace(SearchTextBox.Text) Then
bindingSource.Filter = ""
Else
Dim currentRowCount = bindingSource.Count
bindingSource.Filter = $"TRIM(LastName) = '{SearchTextBox.Text}'"
MessageBox.Show($"Before: {currentRowCount} Now: {bindingSource.Count}")
End If
End Sub
Edit If the column name might be variable consider loading a ComboBox with column names then adjust code as follows.
bindingSource.Filter = $"TRIM({FilterComboBox.Text}) = '{SearchTextBox.Text}'"
In most cases working against a data source is better than working against cells values as per the above recommendation.
I added 2 Exits in the IF statement when the value matches as it's searching each column as well, so it was causing them to be hidden as it loops columns too
For i As Integer = 0 To ContactsList.RowCount - 1
For j As Integer = 0 To ContactsList.ColumnCount - 1
If ContactsList.Rows(i).Cells(j).Value.ToString.ToLower.Trim = ContactsListSearch.Text.ToLower.Trim Then
MsgBox("Item found " + i.ToString)
ContactsList.Rows(i).Visible = True
Else
ContactsList.Rows(i).Visible = False
End If
Next
Next

Move Cursor to another cell in DataGridView - VB.NET

I Have a DataGridView with 6 column.
Column1 = "Product ID" //ReadOnly False
COlumn2 = "Product Name" //ReadOnly True - Auto Fill After Product ID Entered
COlumn3 = "Product Info" //ReadOnly True - Auto Fill After Product ID Entered
Column4 = "Price" //ReadOnly False
Column5 = "Qty" //ReadOnly False
Column6 = "Sub Total" //ReadOnly True - Auto Fill From Price*Qty
How can I only use tab on Cell where the ReadOnly set to False ? I mean, if I aleady input product ID and press "Enter", the cursor / focus move to Column4 and so move to Column5 after I hit "Enter" again in Column4 and add new row after press "Enter" again in Column5.
Allready tried others code but only works for all cell not specific cell.
http://www.vbforums.com/showthread.php?735207-DAtaGridView-move-to-next-column-automatically
Did you try this site? Sorry i cannot post to your comment section because it requires 50 reputation.
I use this so far to make it move to another cell using Enter Keys. Using code from Arsalan Tamiz Blog.
Dim celWasEndEdit As DataGridViewCell
Private _EnterMoveNext As Boolean = True
<System.ComponentModel.DefaultValue(True)>
Public Property OnEnterKeyMoveNext() As Boolean
Get
Return _EnterMoveNext
End Get
Set(ByVal value As Boolean)
_EnterMoveNext = value
End Set
End Property
Private Sub DataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
'if Enter Move Next should work andalso mouse button was NOT down
'we are checking mouse buttons because if select was changed
'by Mouse then we will NOT do our Enter Move Next
If _EnterMoveNext AndAlso MouseButtons = 0 Then
'if selection is changed after Cell Editing
If celWasEndEdit IsNot Nothing AndAlso DataGridView1.CurrentCell IsNot Nothing Then
'if we are currently in the next line of last edit cell
If DataGridView1.CurrentCell.RowIndex = celWasEndEdit.RowIndex + 1 AndAlso DataGridView1.CurrentCell.ColumnIndex = celWasEndEdit.ColumnIndex Then
Dim iColNew As Integer
Dim iRowNew As Integer
'if we at the last column
If celWasEndEdit.ColumnIndex >= DataGridView1.ColumnCount - 2 Then
iColNew = 0 'move to first column
iRowNew = DataGridView1.CurrentCell.RowIndex 'and move to next row
Else 'else it means we are NOT at the last column move to next column
iColNew = celWasEndEdit.ColumnIndex + 1
'but row should remain same
iRowNew = celWasEndEdit.RowIndex
End If
DataGridView1.CurrentCell = DataGridView1(iColNew, iRowNew) ' ok set the current column
End If
End If
celWasEndEdit = Nothing ' reset the cell end edit
End If
End Sub
And for the bypass. I use this (But i don't think its the perfect one, because if i press Left Arrow on Column 4 to move to Column 0, the focus can't move to it, maybe because i put Keys.Sender Right)
Private Sub DataGridView1_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If DataGridView1.Columns(e.ColumnIndex).ReadOnly = True Then
SendKeys.Send("{Right}")
End If
End Sub

Issue Preventing Duplicates from Being Added to a Listbox When Listbox Contains Items Loaded from a Save File

I am experiencing issues preventing duplicates from being added to a listbox.
My Windows Form has 2 listboxes
The form is designed so that when the user clicks a button, the items they have checked in first box are added to the second box
The code below that I have added to the "add" button is intended to prevent the checked item in box 1 from being added to box 2 IF an identical item already exists in box 2.
The problem with my code is that it is not stopping a duplicate from being added to box 2 if box 2 contains items that were loaded from a save file.
Any thoughts on how to fix this issue?
Public Sub additems_Click(sender As Object, e As EventArgs) Handles additems.Click
Dim itemChecked As Object
Dim alreadyonkey As Boolean
Dim duplicates As Integer = 0
If box1.CheckedItems.Count > 0 Then
For Each itemChecked In box1.CheckedItems
alreadyadded = False
'Check if item selected has already been added to box2
If box2.Items.Contains(itemChecked) = True Then
alreadyadded = True
duplicates = duplicates + 1
Else
alreadyadded = False
End If
'Add item if all criteria met
If box2.Items IsNot "" And alreadyadded = False Then
box2.Items.Add(itemChecked)
End If
Next
If duplicates > 0 Then
MsgBox("One or more of the items you are trying to add have already been added.", MsgBoxStyle.Critical, "Item has already been added")
alreadyadded = False
End If
End If
End Sub
I figured out the issue with my code... The issue was primarily due to the fact that a nested For Each loop needed to be used to compare each item in box1 to each item in box2 one at a time and the items needed to be sent to string variables and them compared using "String.Equals".
Public Sub additems_Click(sender As Object, e As EventArgs) Handles additems.Click
Dim itemChecked As Object
Dim alreadyadded As Boolean
Dim duplicates As Integer = 0
If box1.CheckedItems.Count > 0
For Each itemChecked In box1.CheckedItems
Dim itemtoadd As String = itemChecked.ToString
'Check if item selected has already been added to box2
For Each item In box2.Items
Dim box2item As String = item.ToString
If String.Equals(Trim(itemtoadd), Trim(box2item)) = True Then
alreadyadded = True
duplicates = duplicates + 1
Else
End If
Next
'Add item if all criteria met
If itemChecked IsNot "" And alreadyadded = False Then
box2.Items.Add(itemChecked)
End If
Next
If duplicates > 0 Then
MsgBox("One or more of the items you are trying to add have already been added.", MsgBoxStyle.Critical, "Item hase already been added")
alreadyadded = False
duplicates = 0
End If
End If

How to search in listview

I am trying to create a Loop that will read through the information on my ListView through the SubItem to find the text that matches the text in my Textbox whenever I hit the search button and Focuses the listbox onto the matched text. Below is what I have but it keeps telling me that the value of string cannot be converted. I am also pretty sure that my numbers wont loop correctly but I am not really sure how to cause them to loop endlessly till end of statement.
Dim T As String
T = Lines.Text
For r As Integer = 0 to -1
For C As Integer = 0 to -1
If List.Items(r).SubItems(C).Text = Lines.Text Then
List.FocusedItem = T
End If
Next
Next
End Sub
I don't understand your code, but I do understand the question. Below is example code to search all rows and columns of a listview. Search is case insensitive and supports a "find next match" scenario. If a match or partial match is found in any column the row is selected. TextBox1 gets the text to find. FindBtn starts a new search.
Private SrchParameter As String = ""
Private NxtStrtRow As Integer = 0
Private Sub FindBtn_Click(sender As Object, e As EventArgs) Handles FindBtn.Click
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
SrchParameter = TextBox1.Text
NxtStrtRow = 0
SearchListView()
End If
End Sub
Private Sub ListView1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListView1.KeyDown
If e.KeyCode = Keys.F3 Then
SearchListView()
End If
End Sub
Private Sub SearchListView()
' selects the row containing data matching the text parameter
' sets NxtStrtRow (a form level variable) value for a "find next match" scenario (press F3 key)
If ListView1.Items.Count > 0 Then
If SrchParameter <> "" Then
Dim thisRow As Integer = -1
For x As Integer = NxtStrtRow To ListView1.Items.Count - 1 ' each row
For y As Integer = 0 To ListView1.Columns.Count - 1 ' each column
If InStr(1, ListView1.Items(x).SubItems(y).Text.ToLower, SrchParameter.ToLower) > 0 Then
thisRow = x
NxtStrtRow = x + 1
Exit For
End If
Next
If thisRow > -1 Then Exit For
Next
If thisRow = -1 Then
MsgBox("Not found.")
NxtStrtRow = 0
TextBox1.SelectAll()
TextBox1.Select()
Else
' select the row, ensure its visible and set focus into the listview
ListView1.Items(thisRow).Selected = True
ListView1.Items(thisRow).EnsureVisible()
ListView1.Select()
End If
End If
End If
End Sub
Instead of looping like that through the ListView, try using a For Each instead:
searchstring as String = "test1b"
ListView1.SelectedIndices.Clear()
For Each lvi As ListViewItem In ListView1.Items
For Each lvisub As ListViewItem.ListViewSubItem In lvi.SubItems
If lvisub.Text = searchstring Then
ListView1.SelectedIndices.Add(lvi.Index)
Exit For
End If
Next
Next
ListView1.Focus()
This will select every item which has a text match in a subitem.
Don't put this code in a form load handler, it won't give the focus to the listview and the selected items won't show. Use a Button click handler instead.
This is the easiest way to search in listview and combobox controls in vb net
dim i as integer = cb_name.findstring(tb_name.text) 'findstring will return index
if i = -1 then
msgbox("Not found")
else
msgbox("Item found")
end if

How to concat variable integer in control name in vb.net

Now I have a database and pull out that data and display it to form,i have a sequence of groupbox and radiobuttons, in each groupbox (groupbox1,groupbox2,etc...) there are 2 radio buttons namely rdbtn1Yes and rdbtn1No (then it increment +1 in next Groupbox). now i use for loop to go through every groupboxes and radio buttons. And this is my code:
Dim sqlda As New SqlDataAdapter("SELECT * FROM table1 WHERE column1= '" & lblWONo.Text & "'", Constr)
Dim sqlds As New DataSet
sqlds.Clear()
sqlda.Fill(sqlds)
If sqlds.Tables(0).Rows.Count > 0 Then
With sqlds.Tables(0).DefaultView.Item(0)
txtDateCreated.Value = .Item(0).ToString
txtComments.Text = .Item(1).ToString
'check column if it contain FALSE/TRUE value
'then toggle the radiobutton state to TRUE
'In this part i know there is another/easiest way to checked radio buttons to TRUE value
'and this is my code using looping (below):
If .Item(2) = False Then
rdbtn1No.Checked = True
Else
rdbtn1Yes.Checked = True
End If
If .Item(3) = False Then
rdbtn2No.Checked = True
Else
rdbtn2Yes.Checked = True
End If
If .Item(4) = False Then
opt3N.Checked = True
Else
opt3Y.Checked = True
End If
End With
End If
SAMPLE CODE FOR LOOPING:
Dim itemNo As Integer
Dim rdbtnSet As Integer = 1
Dim grpboxCnt As Integer = 1
For Each grpbx As GroupBox In Me.Controls.OfType(Of GroupBox)()
For itemNo = 2 To sqlds.Tables(0).Columns.Count
If .Item(itemNo) = True Then
rdbtn & rdbtnSet & "Yes".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
Else
rdbtn & rdbtnSet & "No".checked = True 'I want to be this way but we know that this is not working or its not the proper way. That is my problem.
End If
Next
rdbtnSet += 1
grpboxCnt += 1
Next
Thats all. Thank you in advance!
Think about the use of a dictionary (id, control) to store your controls. Then iterate the dictionary and set your state.