Hello my code example is :
/*
bdEmpresa (BindingSource)
Contains:
selet idEmpresa,Nombre,Acceso from Empresa
Result
1, Empresa1,true
2, Empresa2,false
3, Empresa2,true
*/
clEmpresas.DataSource = bdEmpresa
clEmpresas.DisplayMember = ? (Nombre)
clEmpresas.ValueMember = ? (Acceso)
This is the code we would use
With clEmpresas.Properties
If .DataSource IsNot Nothing Then .DataSource = Nothing
.DataSource = bdEmpresa
If .DataSource.rows.count = 0 Then Return Nothing 'no data to load
'get the names from the dataset, don't expect them to be specific names
.ValueMember = .DataSource.rows(0).Table.Columns(0).ColumnName
.DisplayMember = .DataSource.rows(0).Table.Columns(1).ColumnName
'Set the selected items, comma separated list of ids
If selectedIds <> "" Then
For i As Integer = 0 To .ItemCount - 1
If Array.IndexOf(selectedIds.Split(","), .GetItemValue(i).ToString) >= 0 Then
.SetItemChecked(i, True)
End If
Next
End If
End With
You can use the CheckMember property to let the control automatically check the selection values from your assigned data source:
e.g.
clEmpresas.DataSource = bdEmpresa
clEmpresas.DisplayMember = "Nombre"
clEmpresas.ValueMember = "idEmpresa"
clEmpresas.CheckMember= "Acceso"
After that you can use the CheckedItems property to get the selected items if user changes selection in the control. see below example:
Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
For Each item As DevExpress.XtraEditors.Controls.CheckedListBoxItem In CheckedListBoxControl1.CheckedItems
MessageBox.Show(item.Value.ToString())
Next
End Sub
Refer these:
get the selected items from a checkedlistbox
How to get checked rows of a data-bound CheckedListBoxControl
Get item index from databound DevExpress CheckedListBoxControl
Related
I have created a table in SQL Server :
its name is [dbo].[Family_Tree]
ID NodeName
1 John
2 George
3 Mike
4 Sandra
5 Jasmine
6 Lucy
I have created a Vb.net Application where I want to present these names in a Combobox or a listbox
Here's the code to fill the Listbox:
Using con As New SqlConnection(My.Settings.ConnString)
Dim DT As New DataTable
Dim Adp As New SqlDataAdapter
With Adp
.SelectCommand = New SqlCommand("Select NodeName , ID From Family_tree", conn)
.Fill(DT)
End With
With ListBox1
.DataSource = DT
.DisplayMember = "NodeName"
.ValueMember = "ID"
End With
End Using
Names are Displayed and nothing is wrong with them , what I want is when I choose any name the listbox returns its corresponding ID .ValueMember = "ID"
, I tested it in a msgbox MsgBox(ListBox1.ValueMember) it always Returns ID , so how can I make it understand that this is a Column not a String ?
thanks in advance...
To get the Value of the current displayed item in the listbox you write
if ListBox1.SelectedValue IsNot Nothing Then
Dim v = Convert.ToInt32(ListBox1.SelectedValue)
.......
ListControl.SelectedValue
Gets or sets the value of the member property specified by the
ValueMember property.
You could use ListBox.SelectedItem, f.e. in it's SelectedIndexChanged event:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim curItem As DataRowView = TryCast(ListBox1.SelectedItem, DataRowView)
If curItem IsNot Nothing Then
Dim id = curItem.Row.Field(Of Int32)("ID")
End If
End Sub
In my winforms application, I loop through the cells of a datagridview and add a tooltip for specific cells based on values in other columns. I do this in the cellformatting event handler, and it worked perfectly. Here is the code:
Private Sub dgvResults_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvResults.CellFormatting
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "TradeInValue"
DirectCast(dgvResults.Rows(e.RowIndex).Cells("TradeInValue"), DataGridViewTextBoxCell).ToolTipText = "Min = " & CDec(dgvResults.Item("BB_Min", e.RowIndex).FormattedValue).ToString("$#####.##") & ", Max = " & CDec(dgvResults.Item("BB_Max", e.RowIndex).FormattedValue).ToString("$#####.##")
If Not IsNothing(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue) AndAlso dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString.Trim.Length > 0 AndAlso CInt(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString) <> -1 Then
If dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Length > 0 Then
Dim ValueParts() As String = dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Split("|")
'Dim selectedTrim As String = ValueParts(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue)
End If
End If
End Select
End Sub
Then, I added code in the cellpainting event handler to hide specific images, again based on values in the datagridview. Here is that code.
Private Sub dgvResults_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "VIN_Pic"
If dgvResults.Rows(e.RowIndex).Cells("VIN_Value").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
Case "EmailDisplayImage"
If dgvResults.Rows(e.RowIndex).Cells("ListingContactEmail").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
End Select
End If
End Sub
When this code as added, the tooltips no longer display. The CellToolTipTextNeeded event fires, and it shows the correct text in the argument, but it never displays. Comment out the lines that assign a new image to the datagridviewimagecells, and the tooltips start displaying again.
I hope this explanation was sufficient. Any ideas?
With the below code, you will run into memory and GDI object leaks very easily as a new Bitmap instance is created every time the cell is painted. Also this is causing your ToolTip to be not displayed.
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
Define the empty bitmap in class level or add as embedded resource and use it.
Dim emptyBitmap As New Bitmap(1, 1);
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = emptyBitmap
Its been nearly a year since I messed with VB, but I am having an issue on my first assignment of the semester which is supposed to be a refresher. What I am supposed to do is make an application that I input a students name, address, GPA, age by textbox, and which year(freshman, sophomore, other) by radio, and classes by checkbox.
Once filled out I need to take that information and have it previewed in a label.text. If it looks right, I need the textbox.text info concatenated into a listbox. No matter what I try or do it either shows String.Array() in the preview or the program crashes at line 57 any help or insight would be appreciated.
Public Class Form1
'CIS259 Spring 2014 Matthew McQuarrie
'Declare Student Data as a String of 9 arrays
Dim StudentData(8) As String
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close application
Me.Close()
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
'Clear entire form
txtName.Clear()
txtAddress.Clear()
txtGPA.Clear()
txtAge.Clear()
radFreshman.Checked = False
radSophomore.Checked = False
radOther.Checked = False
chkCIS.Checked = False
chkMath.Checked = False
chkScience.Checked = False
chkHistory.Checked = False
lblPreview.Text = ""
End Sub
Public Sub btnPreview_Click(sender As Object, e As EventArgs) Handles btnPreview.Click
StudentData(0) = txtName.Text
StudentData(1) = txtAddress.Text
StudentData(2) = txtGPA.Text
StudentData(3) = txtAge.Text
'Find which radio button is checked to add to StudentData
If radFreshman.Checked = True Then
StudentData(4) = "Freshman"
ElseIf radSophomore.Checked = True Then
StudentData(4) = "Sophomore"
ElseIf radOther.Checked = True Then
StudentData(4) = "Other"
End If
'Find which check boxes are checked to add to StudentData
If chkCIS.Checked = True Then
StudentData(5) = "CIS"
If chkMath.Checked = True Then
StudentData(6) = "Math"
If chkScience.Checked = True Then
StudentData(7) = "Science"
If chkHistory.Checked = True Then
StudentData(8) = "History"
End If
End If
End If
End If
'Show StudentData ino the text of lblPreview
lblPreview.Text = StudentData(0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8)
End Sub
Private Sub btnStudent_Click(sender As Object, e As EventArgs) Handles btnStudent.Click
'Add StudentData elements to Student list
lstStudents.Items.Add(StudentData(0 & 1 & 2 & 3))
End Sub
End Class
maybe this approach I used here can help, but there a few differences..
my source is database.. I concatenate the fields in the database (the source)
and I display only the text, not the id on the listbox displaymember. you could concatenate the id to the textfield from the source and displya the fullstring contactenated.
'create the adapter
Dim adapterU As New OleDb.OleDbDataAdapter("Select id,nombre+' '+apellido as fullname from users", con)
'create a datatable
Dim datatableU As New DataTable
'bring the info from the adapter (database) into the datatable.
adapterU.Fill(datatableU)
'relate the datasource (datable) to the listbox lsUsers
lsUsers.DataSource = datatableU
lsUsers.ValueMember = "id"
lsUsers.DisplayMember = "fullname"
I am new to VB.I have a policy no. combobox in VB which populates after I enter first four digits of the policy no.However, if I continue to type the number it overwrites what I have already typed and wipes out the dropdown list, as if selecting for the new 4 numbers I typed.I Want to acheive a scenario where I will enter the first 4 numbers to populate the dropdown after that as i go on entering the policyno the values in the dropdown list get searched.
E.G:Policy number: 969003648, as I type 9690 the drop down is filled in with the policy no's starting from 969000001, now as I go on typing the values as 969003 etc the searched values gets limited to the policy no's having 969003 as starting values...Please assist
My code:
Private Sub PolicyNo_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 46 Or KeyCode = 8 Then
Me.PolicyNo.value = ""
Else
If Len(Me.PolicyNo.Text) >= 4 Then
Me.PolicyNo.RowSource = ""
Call ReloadPolicyNo(Nz(Me.PolicyNo.Text, ""))
Function ReloadPolicyNo(sPolicyNo As String)
Me.PolicyNo.RowSource = "SELECT Inventory.PolicyNo FROM Inventory " & _
"WHERE Left(Inventory.PolicyNo," & Len(Me.PolicyNo.Text) & ") = '" & Me.PolicyNo.Text & "' order by Inventory.PolicyNo"
End Function
Why don't you use the Textchanged event on your combo box to get what is currently in the combobox ?
Example
'THIS FIRES WHEN TEXT IS ENTERED INTO THE COMBOBOX
Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
'GET VALUE IN COMBOBOX
Dim enteredText As String = ComboBox1.Text
Dim strSelect As String = "select * from table where field like = '" & enteredText & "'%"
'load COMBO BOX HERE
End Sub
It's better to use DataView here , I declared DataTable as class Variable.
Dim dt as DataTable
Fetched All the data from database and bind it to declared DataTable, This DataTable further added to DataView and use RowFilter to filter DataView.
Compay_Type is one of the column in my DataTable Below code will filter DataView and bind data where column Company_Type=1
Dim dvComp As New DataView(dt)
dvComp.RowFilter = "Company_Type=1"
ComboBox1.DisplayMember = "CompanyName"
ComboBox1.ValueMember = "CompanyID"
ComboBox1.DataSource = dvComp
To remove Filter just use dvComp.RowFilter = Nothing
I have a ListView control set up in details mode, and on a button press I would like to retrieve all column values from that row in the ListView.
This is my code so far:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim items As ListView.SelectedListViewItemCollection = _
Me.ManageList.SelectedItems
Dim item As ListViewItem
Dim values(0 To 4) As String
Dim i As Integer = 0
For Each item In items
values(i) = item.SubItems(1).Text
i = i + 1
Next
End Sub
But values just comes out as an empty array. Any ideas? I just want the values array to be filled with the data of that ListView row.
Cheers.
Here's the single-line solution:
mylistview.Items.Cast(Of ListViewItem).Select(Function(lvi As ListViewItem)lvi.SubItems(1).Text).ToArray()
You need to iterate the SubItems, not the selected items. Fix:
If Me.ManageList.Items.Count <> 1 Then Exit Sub
Dim row As ListViewItem = Me.ManageList.Items(0)
Dim values(0 To row.SubItems.Count-1) As String
Dim i As Integer = 0
For Each item As ListViewItem.ListViewSubItem In row.SubItems
values(i) = item.Text
i = i + 1
Next
Just to check, are you aware that item.SubItems(1).Text will get the texts from the second column? And that since you're using SelectedItems it'll only look at the currently selected items in the ListView.
Edit:
Also, are you sure there will always just be a maximum of 5 selected items in the ListView?
Otherwise you'll have problems with
Dim values(0 To 4) As String
Old question, I know... maybe this helps for someones reference.. stumbled upon this question in a search for something else.
This works... not sure if the original poster somehow used an empy colelciton of selected items...
Also, dynamically resizing array based on selected items to overcome unforseen bugs...
Dim valueArray(mylistview.SelectedItems.Count - 1) As String
Dim i As Integer
For Each Item As ListViewItem In mylistview.SelectedItems
valueArray(i) = Item.Text
i += 1
Next
To get subitems, use item.subitems(1).text ... subitems(2).text.. etc
Cheers