i just managed to get my ToolstripComboBox to display a Datasource (Dictionary).
But now i want to add a DataBinding to the SelectedValue Property, but i doesn't work.
For a normal ComboBox it works :/..
My Code:
tscbb_Test.ComboBox.DataBindings.Add("SelectedValue", My.Settings, "Setting from mySettings")
Can somebody help?
I just tried this and it worked perfectly for me:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
With table.Rows
.Add(1, "Peter")
.Add(2, "Paul")
.Add(3, "Mary")
.Add(4, "John")
End With
Me.BindingSource1.DataSource = table
Me.BindingSource2.DataSource = table
With Me.ToolStripComboBox1.ComboBox
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = Me.BindingSource1
.DataBindings.Add("SelectedValue", My.Settings, "ToolStripSelectedValue")
End With
With Me.ComboBox1
.DisplayMember = "Name"
.ValueMember = "ID"
.DataSource = Me.BindingSource2
.DataBindings.Add("SelectedValue", My.Settings, "FormSelectedValue")
End With
End Sub
I could run the project, select a different item in each ComboBox, close the project, run it again and the items I previously selected were selected again, indicating that the settings must have been saved.
I just tried this code with two settings of type Keys and it worked as expected:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim keys = [Enum].GetValues(GetType(Keys))
Me.BindingSource1.DataSource = keys
Me.BindingSource2.DataSource = keys
With Me.ToolStripComboBox1.ComboBox
.DataSource = Me.BindingSource1
.DataBindings.Add("SelectedItem", My.Settings, "ToolStripSelection")
End With
With Me.ComboBox1
.DataSource = Me.BindingSource2
.DataBindings.Add("SelectedItem", My.Settings, "FormSelection")
End With
End Sub
Related
I have a form with two DataGridViews and two BindingSources
This is a vb.net project on Windows 7 with Visual Studio 2019
Here is where things go wrong. When I do an Edit Columns on dgvTwo where I remove all the Columns this causes both DataGridViews to load nothing
The first DataGridView (dgvData) never had Columns Added with the RIGHT CLICK routine
But if I use Add Columns by RIGHT CLICK Add Columns to dgvTwo and run the code here is a screen shot
The Question is how to load the data in two DataGridViews with two BindingSources?
And the Code is Below
Public Class frmOne
Private Sub frmOne_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table1 As New DataTable
Dim table2 As New DataTable
With table1.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
With table1.Rows
.Add(1, "Peter")
.Add(2, "Paul")
.Add(3, "Mary")
End With
With table2.Columns
.Add("ID", GetType(Integer))
.Add("Word", GetType(String))
End With
With table2.Rows
.Add(1, "Bob")
.Add(2, "Carol")
.Add(3, "Alice")
End With
'This code works if you do not use table2 code
'dgvTwo.Rows.Add(1, "Bob")
'dgvTwo.Rows.Add(2, "Carol")
'dgvTwo.Rows.Add(3, "Alice")
For Each column As DataGridViewColumn In dgvTwo.Columns
column.SortMode = DataGridViewColumnSortMode.NotSortable
Next
dgvTwo.DefaultCellStyle.Font = New Font("Tahoma", 15)
dgvTwo.Columns(0).Width = 100
dgvTwo.Columns(1).Width = 150
dgvData.GridColor = Color.Red
dgvData.CellBorderStyle = DataGridViewCellBorderStyle.None
dgvData.BackgroundColor = Color.LightGray
dgvData.DefaultCellStyle.SelectionBackColor = Color.DarkGray
dgvData.DefaultCellStyle.SelectionForeColor = Color.Black
dgvData.DefaultCellStyle.WrapMode = DataGridViewTriState.[True]
dgvData.SelectionMode = DataGridViewSelectionMode.FullRowSelect
dgvData.AllowUserToResizeColumns = False
dgvData.RowsDefaultCellStyle.BackColor = Color.White
dgvData.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
Me.bsOne.DataSource = table1
Me.dgvData.DataSource = Me.bsOne
Me.bsTwo.DataSource = table2
Me.dgvTwo.DataSource = Me.bsTwo
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
Using dialogue As New frmTwo
Dim selectedRecord = DirectCast(Me.bsOne.Current, DataRowView)
'Pass the initial data into the dialogue.
dialogue.TextBoxText = CStr(selectedRecord("Name"))
'Display the modal dialogue.
If dialogue.ShowDialog() = DialogResult.OK Then
'The user clicked OK so get the final data from the dialogue.
selectedRecord("Name") = dialogue.TextBoxText
Me.bsOne.EndEdit()
End If
End Using
End Sub
Screen Shot
I have a bounded Combobox and it's data source is sorted but it seems that auto complete re-sort its items when I start typing.
For example I use this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
With dt
.Columns.Add("id", GetType(Long))
.Columns.Add("Name", GetType(String))
.Rows.Add({1, "John"})
.Rows.Add({2, "Jan"})
End With
With Me.ComboBox1
.DataSource = dt
.ValueMember = "id"
.DisplayMember = "Name"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub
Now when I type j, the Combobox suggest Jan but I want the first item John.
For unbounded Combobox, this works: How to provide automatic text completion for a ComboBox control in Visual Basic .NET or in Visual Basic 2005 and also this question is For unbounded Combobox.
I modified the code in this link and now it works as needed:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
With dt
.Columns.Add("id", GetType(Long))
.Columns.Add("Name", GetType(String))
.Rows.Add({1, "John"})
.Rows.Add({2, "Jan"})
End With
With Me.ComboBox1
.DataSource = dt
.ValueMember = "id"
.DisplayMember = "Name"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.None
End With
End Sub
Private Sub ComboBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
' Do nothing for some keys such as navigation keys.
If ((e.KeyCode = Keys.Back) Or
(e.KeyCode = Keys.Left) Or
(e.KeyCode = Keys.Right) Or
(e.KeyCode = Keys.Up) Or
(e.KeyCode = Keys.Delete) Or
(e.KeyCode = Keys.Down) Or
(e.KeyCode = Keys.PageUp) Or
(e.KeyCode = Keys.PageDown) Or
(e.KeyCode = Keys.Home) Or
(e.KeyCode = Keys.End)) Then
Return
End If
Dim actual As String = Me.ComboBox1.Text
Dim dt As DataTable = Me.ComboBox1.DataSource
Dim dr = dt.Select("Name like '" & actual & "%'")
If dr.Count > 0 Then
Dim found As String
With Me.ComboBox1
.DroppedDown = True
Cursor.Current = Cursors.Default 'to show the cursor again
found = dr(0)("Name")
.Text = found
.SelectionStart = actual.Length
.SelectionLength = found.Length
End With
End If
End Sub
I added this line Cursor.Current = Cursors.Default to resolve this problem.
I have a DatagridviewComboBoxColumn populated from a DataTable and whenever I click on any part of the DataGridViewComboBoxCell the first value of the list shows up as it had been clicked. However, when I move the focus to another cell without selecting a value, it disappears.
Strangely, the behavior is not consistent if I apply the ComboBox values with .Items.Add(" "). Can anyone shed some light on this issue.
Here is a sample code and a gif image:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add()
DataGridView1.Rows.Add()
''DataGridViewComboBoxColumn1
DataGridViewComboBoxColumn1.Items.Add("Name1")
DataGridViewComboBoxColumn1.Items.Add("Name2")
'DataGridViewComboBoxColumn2
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Rows.Add("1", "Name1")
dt.Rows.Add("2", "Name2")
With DataGridViewComboBoxColumn2
.ValueMember = dt.Columns(0).ColumnName
.DisplayMember = dt.Columns(1).ColumnName
.DataSource = dt
End With
End Sub
End Class
Successfully replicated the behavior, but i also added 2 ComboBox to the same form and the result is same
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DataGridView1.Rows.Add()
DataGridView1.Rows.Add()
''DataGridViewComboBoxColumn1
DataGridViewComboBoxColumn1.Items.Add("Name1")
DataGridViewComboBoxColumn1.Items.Add("Name2")
'DataGridViewComboBoxColumn2
Dim dt As New DataTable
dt.Columns.Add("id")
dt.Columns.Add("name")
dt.Rows.Add("1", "Name1")
dt.Rows.Add("2", "Name2")
With DataGridViewComboBoxColumn2
.ValueMember = dt.Columns(0).ColumnName
.DisplayMember = dt.Columns(1).ColumnName
.DataSource = dt
End With
ComboBox1.Items.Add("Name1")
ComboBox1.Items.Add("Name2")
With ComboBox2
.ValueMember = dt.Columns(0).ColumnName
.DisplayMember = dt.Columns(1).ColumnName
.DataSource = dt
End With
End Sub
End Class
Click to see gif of form in action
I found a way to correct the behavior by referring to this post:
DataGridComboBoxColumn shows first value on CellEnter
Here is the code I have used:
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim comboBox As ComboBox = DirectCast(e.Control, ComboBox)
If DataGridView1.CurrentCell.Value Is Nothing Then comboBox.SelectedIndex = -1
End If
End Sub
I have a ComboBox with DropDownStyle = Simple and have bound it to a DataSet. What I want is : when I type 'a', I want my ComboBox to show only the items starting with the letter 'a' just Dictionary Program.
I have tried the AutoComplete property but it just shows a DropDown and that's not what I want
HERE IS MY CODE:
Private Sub TICKETING_FORM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "STYLE")
bs.DataSource = ds.Tables("STYLE")
With Style_ComboBox
.DataSource = bs
.DisplayMember = "STYLE"
.ValueMember = "STYLE"
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub
You need to set these property for AutoComplete
ComboBox1.AutoCompleteMode = AutoCompleteMode.Append
ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
Private Sub TICKETING_FORM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "STYLE")
bs.DataSource = ds.Tables("STYLE")
With Style_ComboBox
.DataSource = bs
.DisplayMember = "STYLE"
.ValueMember = "STYLE"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub
Change this Setting
DropDownStyle = DropDownList
Private Sub TICKETING_FORM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "STYLE")
bs.DataSource = ds.Tables("STYLE")
With Style_ComboBox
.DataSource = bs
.DisplayMember = "STYLE"
.ValueMember = "STYLE"
.AutoCompleteMode = AutoCompleteMode.SuggestAppend ' This is necessary
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
End Sub
The answers is quite simple. You load your combobox like you would normally by adding the display member and the value member. Then you load your autocomplete by the Display member and whala. Keep in mind that the auto complete function is basicly just a filter option on the display member side under normal conditions.
Here follows and example. Keep in mind that this example draws the list of names from our AD account and is nor shown here....
Dim col As New AutoCompleteStringCollection
Dim i As Integer
If Not MyUsers Is Nothing Then
For i = 0 To MyUsers.Rows.Count - 1
col.Add(MyUsers.Rows(i)("displayname").ToString)
Next
Request_ManagerComboBox.AutoCompleteSource = AutoCompleteSource.CustomSource
Request_ManagerComboBox.AutoCompleteCustomSource = col
Request_ManagerComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
Request_ManagerComboBox.DisplayMember = MyUsers.Rows(i - 1)("displayname").ToString
Request_ManagerComboBox.ValueMember = MyUsers.Rows(i - 1)("samAccountName").ToString
End If
I have a datagridview with bindingsource contains three columns but one of them contains ComboBoxes.
with code below I can write to xml file but how can I read from it ?
Appreciate your help
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Me.BindingSource2.DataSource = Me.GetParentTable()
Me.Column1.DisplayMember = "ParentName"
Me.Column1.ValueMember = "ParentID"
Me.Column1.DataSource = Me.BindingSource2
Me.BindingSource1.DataSource = Me.GetChildTable()
With DataGridView1
.DataSource = Me.BindingSource1
.AllowUserToAddRows = True : .AllowUserToDeleteRows = True
.AllowUserToOrderColumns = False : .AllowUserToResizeRows = True
End With
End Sub
Private Function GetParentTable() As DataTable
Dim table As New DataTable
With table.Columns
.Add("ParentID", GetType(Integer))
.Add("ParentName", GetType(String))
End With
With table.Rows
.Add(1, "Parent 1")
.Add(2, "Parent 2")
.Add(3, "Parent 3")
End With
Return table
End Function
Private Function GetChildTable() As DataTable
Dim table As New DataTable ("IOBOARD")
With table.Columns
.Add("ChildID", GetType(Integer))
.Add("ParentID", GetType(Integer))
.Add("ChildName", GetType(String))
End With
With table.Rows
.Add(1, 3, "Child 1")
.Add(2, 2, "Child 2")
.Add(3, 1, "Child 3")
End With
Return table
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Save To XML
Dim dt As DataTable = CType(BindingSource1.DataSource, DataTable)
dt.AcceptChanges()
dt.WriteXml("c:\so\Test.xml", System.Data.XmlWriteMode.WriteSchema, False)
End Sub
End Class
Have you tried the dataset.ReadXml method?