Select only 1 item from Listview - vb.net

I have some code that reads the value of a Listview on a button click. However, what it does is select all the entries to add to another Listview. How do I adjust my code to allow for single items selection. Thanks
lvSelectRequestItems.Items.Clear()
While dr.Read()
Dim LVS As New ListViewItem
'LVS.SubItems.Clear()
With (LVS)
.UseItemStyleForSubItems = False
.Text = dr("Box").ToString()
.SubItems.Add(dr("CustRef").ToString())
End With
lvSelectedItems.Items.Add(LVS)
End While

If ListViewObj.SelectedItems.Count >= 1 Then
While ListViewObj.SelectedItems.Count >= 1
ListViewObj.SelectedItems(0).Selected = False
End While
ListViewObj.Items(ListViewObj.FocusedItem.Index).Selected = True
End If

Related

How To Update A Janus GridEx Checkbox At Runtime

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

Check all checkboxes in a listview

I want to have a button that will check all checkboxes in my listview. I have:
Dim I as Integer
If listViewAccounts.CheckedItems.Count > 0 then
(>>My Problem here<<)
End if
What to do next?
for i = 0 to listViewAccounts.Items.Count -1
listViewAccounts.Items(i).Checked = true
next
Or just use Linq
listView1.Items.OfType(Of ListViewItem).All(Function(c)
c.Checked = True
Return True
End Function)
I used:
For Each lvi As ListViewItem In listViewAccounts.Items
lvi.Checked = True
Next

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.

Resetting Datagridview combo box data at runtime

I am creating grid on form load event.
Initially I am setting some values in datagridview combobox as :
Dim dgvc As DataGridViewComboBoxCell
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn1.Text \\setting selected item
datagrigview1.Rows(0).Cells("Column1").Value = txtColumn2.Text
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" ")) \\adding blank
dgvc.Items.Add(txtColumn1.Text) \\then required value
dgvc = datagrigview1.Rows(0).Cells("Column1").Value
dgvc.Items.Add((" "))
dgvc.Items.Add(txtColumn2.Text)
Now when user clicks on particular combobox.I am setting new values in it as :
// Resetting old values
If IsDBNull(dgvc) = False Then
dgvc.DataSource = Nothing
dgvc.Items.Clear()
End If
If DtTable.Rows.Count > 0 Then
Dim k As Integer
Dim dgvc1 As DataGridViewComboBoxCell
dgvc1 = New DataGridViewComboBoxCell()
For k = 0 To DtTable.Rows.Count - 1
If DtItemCd.Rows(k)("ItemCd").ToString <> Current_Code Then
datagrigview1.Rows(e.RowIndex).Cells("Column1").Value = DtTable.Rows(k)("Column1").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column1")
dgvc1.Items.Add(DtTable.Rows(k)("Column1").ToString)
datagrigview1.Rows(e.RowIndex).Cells("Column2").Value = DtTable.Rows(k)("Column2").ToString
dgvc1 = datagrigview1.Rows(e.RowIndex).Cells("Column2")
dgvc1.Items.Add(DtTable.Rows(k)("Column2").ToString)
End If
Next
End If
This shows both old and new records.Please help.
Probably your code here
If IsDBNull(dgvc) = False Then
is testing if the DataGridViewComboBoxCell is Null not if it's DataSource is null.
Hence it never enters the condional code below.
Could you try to change in this way and see if now you enter in the if condition?
If IsDBNull(dgvc.DataSource) = False Then
I got below solution :
If IsDBNull(dgvc) = False Then
dgvc.Items.Clear()
dgvc.DataSource = Nothing
dgvc = dgvSO.Rows(e.RowIndex).Cells("Column1")
dgvc.Items.Remove(" ")
dgvc.Items.Remove(Current_Code)
End If
This works
cb.SelectedItem = Nothing

Gridview Show and Hide a Specific Column

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