Display data in combobox from one winform to another winform - vb.net

I have this form1 that displays a list of my entries. When I want to update an entry, I will click it in the DataGridView and an update button will be visible. Once I clicked the 'Update' button, form2 will appear and the details of the currently selected row in my dgv will be listed.
My problem is that one of the details involves a data from a combobox. It does not change the value.
This form2 acts as a form to create entries and at the same time to update them. I load the details to this form as is like you are creating one but I am updating them.
Now this combobox has data that has been loaded from the database. Something like this:
Dim cmd2 As New MySqlCommand
Dim dt2 As New DataTable
con.Open()
With cmd2
.Connection = con
.CommandText = "Select staff_name from staff where staff_position='Pre-seller'"
End With
da.SelectCommand = cmd2
da.Fill(dt2)
With cmbseller
.DataSource = dt2
.DisplayMember = "staff_name"
.ValueMember = "staff_name"
End With
con.Close()
All is well, until I need to update an entry. The data in the combo box is not changing from the details of my currently selected row.
For example, the data in the entry for this combobox is 'Leizel', but when I click the update to show form2, it shows a different item. Like the one that has been loaded from the database. It is not adjusting to what I want. I change the other details but not this one.
Can anyone suggest anything?
EDIT:
This is the code I am using in loading data from form1 to form2. obviously this is only a part of it.
Dim frm3 As New SalesOrder
frm3.cmbseller.Text = DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value
where 2 is the column in my dgv that needs to be displayed in the combobox in form2.

Related

read from SQL to Button

please help on this issue, i want to read ID from SQL to different button but still i cant able to do it. please help.
this is the code i tried.
this code read same ID in both button instead of different id in different button. see the screenshot for your reference.
Dim dt As New DataTable
Dim da As New SqlDataAdapter("select Locker_ID from Locker", colnnn)
da.Fill(dt)
For Each row As DataRow In dt.Rows
Button2.Text = row.Item("Locker_ID")
Button1.Text = row.Item("Locker_ID")
Next
You do not only have to loop the rows but also the buttons. If you have 2 locker rows, your loop executes the loop body twice. I.e., the first locker id is assigned to both buttons and then the second locker id is assigned to the same buttons again, overwriting the first value.
Dim dt As New DataTable
Dim da As New SqlDataAdapter("select Locker_ID from Locker", colnnn)
da.Fill(dt)
Dim i As Integer = 1
For Each row As DataRow In dt.Rows
Controls("Button" & i).Text = row.Item("Locker_ID")
i = i + 1
Next
This will assign the first Locker_ID to Button1, the second one to Button2, etc.
The Controls collection allows you to access the controls by name by using a string as index.
Note that you can change the name of the buttons. If you name the buttons where you want to have the locker id as btnLocker1, btnLocker2, btnLocker3 etc. then Controls("btnLocker" & i).Text = row.Item("Locker_ID") will automatically select the right buttons.
It is advisable to give your controls speaking names. Also, do this before creating event handlers. E.g., nobody knows what Sub Button17_Click is supposed to do; however, if you name the button btnPrint, the winforms designer will automatically create a Sub named btnPrint_Click when you double click the button.
See also: For Each Loops

Open Form faster & remove "System.Data.DataRowView" flicker from Combobox data binding

I have plenty Comboboxes on my form (around 20), and all of them are displaying items from different tables of my DB. If I put all code on Form_Load event then Form opens very slow. So I tried to paste code in different varieties, and currently I'm stuck at Combobox_Enter event - now Form loads fast, but when I click on drop-down of combobox I see sometimes "System.Data.DataRowView" flickering before items are loaded in Combobox. Is there any way to achieve both - fast Form opening & Combobox loading Items without flickering ?....So far I tested with Form_Activate,Form_GotFocus(not working) and Combobox_GotFocus,Combobox_MouseHover,Combobox_Click(not exactly perfect). This is an example of how I bind my Comboboxes:
Private Sub Combobox1_Enter(sender As Object, e As EventArgs) Handles Combobox1.Enter
Dim SQL As String = "SELECT Name from MyTable"
Dim dtb As New DataTable()
Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")
Try
con.Open()
Using dad As New OracleDataAdapter(SQL, con)
dad.Fill(dtb)
End Using
Combobox1.DataSource = dtb
Combobox1.DisplayMember = "Name"
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
Combobox1.SelectedIndex = -1
End Using
End Sub
I also tried with declaring " Public con As OracleConnection", but output is same as I have It now.
Any help appreaciated !
When binding a ComboBox or the like, you should pretty much ALWAYS set the DataSource last. You are not and that's why you see "System.Data.DataRowView" displayed.
When you bind a list to a ComboBox, the control will display data from the column or property specified in the DisplayMember if there is one, otherwise it will call ToString on each item. In your code, you first set the DataSource and, at that point, the DisplayMember is not set so the control calls ToString on each item. The result of that is "System.Data.DataRowView". When you then set the DisplayMember, those values that the control just went to the trouble of generating and displaying are discarded and the DisplayMember is used to get new values.
Even if you weren't seeing that effect, you'd still be wasting your control's time generating values that you don't want. ALWAYS set the DisplayMember, ValueMember or the like before setting the DataSource in code unless you have a specific reason not to. The only reason that I'm aware of is when you're binding a CheckedListBox, which has an issue when DataSource is set last.
By the way, shouldn't you have a test there to only retrieve data if there is no data already loaded? You don't want to reload data if the user returns to the same control, do you?

How to run code in real time across multiple tabs, windows form, vb.net

I am currently programming in vb.net for a windows forms applications.
I have a windows form with multiple tabs and within each tab I have a DGV. Each DGV is connected to an sql server table, the same table across all tabs.
I want to use a check box to copy rows of data from the sql table to a specific tab. In this case I want to try and have this happen in real time, so without using a "refresh" button or something like that.
On each row in every tab I want the row of data to appear or disappear from the last tab depending on the status of the check box.
I have attached code and a picture. The code is used on the form load event to load the tables into the tabs. In the image you can see i want the rows to be loaded into the "ordered floors" tab after the check box has been checked.
Private Sub FormOrdered_Load(sender As Object, e As EventArgs) Handles Me.Load
'load line 2 tab
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As SqlCommand = New SqlCommand("Select LineNumber, ShearNumber, JobNumber, FloorNumber, OrderedBy, DateOrdered, Ordered FROM Production.dbo.tblFCordered where LineNumber = 2", conn1)
Dim da As New SqlDataAdapter
da.SelectCommand = comm1
da.Fill(Line2)
End Using
conn1.Close()
End Using
DataGridLine2.DataSource = Line2
Catch ex As Exception
MsgBox("Unable to make SQL Connection to load Line 2 Table, please contact an engineer!")
MsgBox(ex.ToString)
End Try
image
You could just clone the rows from the DGV when you check the checkbox and add them to the Ordered floors dgv.
Something like:
Private Sub CopyRows_CheckedChanged() Handles CopyRows.CheckedChanged
For each r as DataGridViewRow in MyDGV.Rows
dgvOrderedFloors.Rows.Add(r.Clone())
next
End Sub
You'll have to modify this to suit obviously, but I think something along these lines should do what you want.
Or you could clone the data tables you use for each datagridview, and add them to one master table which will be the datasource for your Ordered floors datagridview.
That would be better, but might be more work upfront.

combobox text still showing previous value on change

I am using 1 combobox to populate a 2nd combobox. What is happening however, is that the 2nd combobox dropdown is changing and displaying the correct values but when the change occurs, the text property still retains the old value. I am using items.clear() and thought that would also clear the text value of the combobox.
I am using winforms and would be grateful if someone could point out my newbie error. Thanks
Private Sub cmbCustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbCustomer.SelectedIndexChanged
' This is the routine to populate the departments box with values from customer
sql = "SELECT * from Departments WHERE Customer = ?"
Dim cmd As New OleDb.OleDbCommand
cmd.Parameters.AddWithValue("#p1", cmbCustomer.Text)
cmd.CommandText = sql
cmd.Connection = oledbCnn
dr = cmd.ExecuteReader
cmbDept.Items.Clear()
cmbRequestBy.Items.Clear()
While dr.Read()
cmbDept.Items.Add(dr("Name"))
End While
cmd.Dispose()
dr.Close()
'oledbCnn.Close()
End Sub
You are clearing the Items collection not the Text property of the combo.
This behavior depends on the DropDownStyle property of the combobox.
The default is DropDown that allow the combo to maintain separate inner controls for Items (A List of some kind) and Text (a TextBox)
If you change the property to DropDownList clearing the Items collection will clear also the 'text' part of it.
Of course you loose the possibility to type inside the TextBox portion of the combobox for example to add an item not included in the list
See Microsoft Connect for Microsoft Response to similar question

vb.net Bind a combobox to a datasource

Over the past few days, I've read about a thousand forum posts, and looked at many code samples, but I still can't figure this out.
My goal is to populate a combobox with values from an Access 2007 query. I can do this by using the DataSource, DisplayMember and ValueMember properties, but regardless of the data in the query, the comboboxes all just default to the to first item in the items collection and don't change when the main BindingSource is moved. I am binding the controls with this line of code:
ComboBox1.DataSource = DataSet1.qryItemSourceTest
ComboBox1.DisplayMember = "SourceTestDisplayField"
ComboBox1.ValueMember = "SourceTestIDField"
Combobox1.DataBindings.Add(New Binding("SelectedValue", qryTestBindingSource, "TestField", True))
I have also tried using a BindingSource as a DataSource.
I have also tried using an array as a DataSource.
If I drag the control on from the Data Source tab in the IDE, it will scroll through records properly, but it will only display the value and not the query 'look-up' value that I want the combobox to display. I have tried changing the DisplayMember and ValueMember properties of the combobox after dragging it on from the Data Sources tab and that seems to break the functionality as well.
I'm looking for some best practices here. I know I'm missing something easy here and I apologize for post an issue that has already been covered so many times, but I could use some individual help here.
Edit: I eventually was able to accomplish my goal using the Visual Studio IDE Properties window. The second property on a control is a Data Bindings property which expands into exactly what I needed. I just never saw this before because it was not in alphabetically order like everything else.
Hi you can do this code to populate data into combo
Try
Dim cn As New OleDbConnection
cn.ConnectionString = conString
cn.Open()
cmd.Connection = cn
cmd.CommandText = "your query"
'Execte reader function is used to hold more than one value from the table
dr = cmd.ExecuteReader()
' Fill a combo box with the datareader
Do While dr.Read = True
ComboBox1 = dr.Item(0)
Loop
cn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
If you have any more problems don't hesitate to ask.