Refresh/Requery combobox on form after record saved on separate form - vb.net

I have spent all morning googling and looking through this site and just haven't found anything that seems to work. I have a combobox on form1 that gets its display values from a datasource tblCardTypeDD. Its Display Member is sCardType and its value member is iCardTypeID. Its selected member is fkCardTypeID from tblInventory.
When I click a button form2 pops up with a simple datagridview that allows me to add more card types to be used in the drop down. Currently I have been trying to add code to the save button click method but I just can't find a combination that actually updates the combobox.
I have been trying the following:
frmInventory.SEquipTypeComboBox.DataSource = Nothing
frmInventory.SEquipTypeComboBox.DataSource = Me.EngDBbeDataSet.tblCardTypeDD
but the drop down becomes blank after execution of the above.
Any help would be appreciated.

You should check this page, maybe it'll help you:
http://msdn.microsoft.com/en-us/library/w67sdsex.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
In case it didn't:
if you put your breakpoint where you "reset" The combobox Datasource binding, Check if the datasource contains the new values you want to see in your combobox. it might sound stupid but its a mistake that often happens, if this is the case you know that the problem isn't with the binding
Also after making the datasource nothing and adding the datasource again in the way you did you should tell which column will be the displaymember again like so:
ComboBox1.DisplayMember = "Column1"
Though if you look at the site I told you about you should look at the note that tells you how to suspend and resume binding instead of dropping the datasource and adding it again like that.
Though if you're using a datatable the values should automatically change, and there should be no reason to drop and rebind the combobox.
--
To prove that you don't need to rebind i've very quickly made a very dirty piece of code to prove it.
form1 has a combobox that's bound to a datatable, and it also has a button to call a second form named form2.
form2 has a button that adds 2 rows to the Datatable and then goes back to the first screen.
Code Form 1:
Public Class Form1
Private dataSet As New DataSet("DataSet1")
Private dataTable As DataTable = dataSet.Tables.Add("DataTable1")
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dataTable.Columns.Add("Column1")
Dim row1 As DataRow = dataTable.NewRow()
row1("Column1") = "Data1"
dataTable.Rows.Add(row1)
Dim row2 As DataRow = dataTable.NewRow()
row2("Column1") = "Data2"
dataTable.Rows.Add(row2)
ComboBox1.DataSource = dataTable
ComboBox1.DisplayMember = "Column1"
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim form2 As New Form2(dataTable)
form2.Show()
End Sub
End Class
Code Form2:
Public Class Form2
Private _dataTable As DataTable
Public Sub New(ByVal dataTable As DataTable)
InitializeComponent()
_dataTable = dataTable
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim row3 As DataRow = _dataTable.NewRow()
row3("Column1") = "Data3"
_dataTable.Rows.Add(row3)
Dim row4 As DataRow = _dataTable.NewRow()
row4("Column1") = "Data4"
_dataTable.Rows.Add(row4)
Me.Close()
End Sub
End Class

Related

OnSelectedIndexChanged no working in aspx Page

i have this dropdownlist in my page
<asp:DropDownList ID="ddlselectTaxas" runat="server" AutoPostBack="True" CssClass="form-control" OnSelectedIndexChanged="ddlselectTaxas_SelectedIndexChanged"
EnableViewState="True" ViewStateMode="Enabled"></asp:DropDownList>
and this is the function in the vb file
Public Sub ddlselectTaxas_SelectedIndexChanged(sender As Object, e As EventArgs)
taxas.TryGetValue(ddlselectTaxas.SelectedItem.Text, valorTxt.Text)
valorTxt.Text = valorTxt.Text.Substring(0, valorTxt.Text.Length - 2)
End Sub
I want to change the value of a textbox (valorTxt) when I select a diferent value in the dropdownlist, but the function is not firing. I dont know why, i have zero experience with VB and asp pages. I apreciate your help. thanks in advance.
EDIT
here is the full code of the page
Public Class Emitir
Inherits System.Web.UI.Page
Public taxas As New Dictionary(Of String, String)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
CarregaValores()
valorTxt.ReadOnly = True
End If
End Sub
Private Sub CarregaValores()
Dim dt As DataTable
Dim objAcesso As New AcessoBD
Dim consulta As String = "SELECT TE.COD_TAXA, TE.DESC_TAXA, TV.VL_TAXA FROM TAXAS_EXPEDIENTE TE JOIN TAXAS_EXPEDIENTE_VALOR TV ON TE.COD_TAXA = TV.COD_TAXA"
dt = objAcesso.DataTable(consulta, CommandType.Text)
For Each linha As DataRow In dt.Rows
taxas.Add(linha.ItemArray(1), linha.ItemArray(2))
Next
valorTxt.Text = dt.Rows.Item(0).ItemArray(2)
valorTxt.Text = valorTxt.Text.Substring(0, valorTxt.Text.Length - 2)
ddlselectTaxas.DataTextField = "DESC_TAXA"
ddlselectTaxas.DataValueField = "COD_TAXA"
ddlselectTaxas.DataSource = dt
ddlselectTaxas.DataBind()
End Sub
Protected Sub ddlselectTaxas_SelectedIndexChanged(sender As Object, e As EventArgs)
taxas.TryGetValue(ddlselectTaxas.SelectedItem.Text, valorTxt.Text)
valorTxt.Text = valorTxt.Text.Substring(0, valorTxt.Text.Length - 2)
End Sub
End Class
You don't show your code to load up the drop list.
Remember, your page load code fires EVERY time on post back.
So, if in code on page load, you load up the drop list? You need to do this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData()
LoadCal()
End If
End Sub
Sub LoadData()
Dim cmdSQL As New SqlCommand(
"SELECT ID,(FirstName + ' ' + LastName) as EmpName
FROM Employee ORDER BY FirstName")
lstEmployee.DataSource = MyRstP(cmdSQL)
lstEmployee.DataBind()
End Sub
Function MyRstP(cmdSQL As SqlCommand) As DataTable
Dim rstData As New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Using cmdSQL
cmdSQL.Connection = conn
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
Now, in above, I was using a listbox, but they work the same as a drop down list (combo box).
but, if you do NOT place the loading up of data inside of the If Not IsPostBack?
Then what happens is you select a drop down, and say click on a button (or in your case autopostback = true, then the page load FIRES FIRST and THEN your code runs!!!
and, if your code re-loads the drop list? Then your selection is lost!!!
So, for just about ANY page you create, ALWAYS, but always use the is postback code stub in on-load - and thus your loading of controls data ONLY occurs one time. Since if you re-load every time, then you lose your changes.
Well, I am not sure of what was happening, but I remade my page and it is working now. something was blocking the event. Thanks for your help.

Add data into new added row in `DataGridView` from `DataBindingSource`

In Form4 i have a DataGridView named DbTableDataGridView.
In Form3 there is a set of fields (text boxes) that are all bound to the DbTableBindingSource . When I run application the Form4 shows up. There is a button to open new form (Form3) and in there enter details about customers to be added as new row into database (DataGridView). My code for the "Add" button in Form4 looks like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.DbTableDataGridView.Refresh()
Me.DbTableBindingSource.AddNew()
Form3.ShowDialog()
Form3.ImiéTextBox.Text = ""
Form3.NazwiskoTextBox.Text = ""
Form3.Numer_TelefonuTextBox.Text = ""
Form3.Numer_RejestracyjnyTextBox.Text = ""
Form3.MarkaTextBox.Text = ""
Form3.ModelTextBox.Text = ""
Form3.Poj_SilnikaTextBox.Text = ""
Form3.RocznikTextBox.Text = ""
Form3.PaliwoTextBox.Text = ""
Form3.Data_PrzyjeciaDateTimePicker.Value = DateTime.Now
Form3.RichTextBox1.Text = ""
End Sub
It does add new row, selects it and clears entries in the text boxes (that are bound into 'DbTableBindingSource'.
In this form after I fill in all the fields I press button save:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Me.Validate()
Form4.DbTableBindingSource.EndEdit()
Me.DbTableTableAdapter.Update(CartronicDBDataSet.dbTable)
TableAdapterManager.UpdateAll(CartronicDBDataSet)
DbTableTableAdapter.Fill(Form4.CartronicDBDataSet.dbTable)
MsgBox("Saved")
Catch ex As Exception
MessageBox.Show("Blad zapisu. Sprobuj ponownie. W razie potrzeby zamknij, a nastepnie uruchom ponownie program Cartronic")
End Try
End Sub
It goes to the message "Saved" but actually does not fill in added new recently.
Any thoughts?
There is no link that I can see between Form3 and your data.
I'd recommend passing the newly created data row to a new instance of Form3.
Get the reference to your newly added row
Create a new Form3 (avoid default instance forms, they're ugly and evil). This will mean you don't need to clear the textboxes as you have a brand new form every time.
Pass the datarow into your form where you will bind it directly to the textboxes
Dim newRow = CType(Me.DbTableBindingSource.AddNew(), DataRow)
Using frmEditor As New Form3
frmEditor.DataSource = newRow
frmEditor.ShowDialog()
End Using
In form3 add the property (or preferably a constructor)
Private mDataSource As DataRow
Public Property DataSource As DataRow
Get
Return mDataSource
End Get
Set(value As DataRow)
mDataSource = value
Me.ImiéTextBox.DataBindings.Add("Text", mDataSource, "ImiéFieldName")
' ....
End Set
End Property
If you use this approach then you can get rid of all of the textbox clearing code.
I have done what you have suggested but little bit simpler.
Assigned all text boxes to each cell in current row as follows:
Form4.DbTableDataGridView.CurrentRow.Cells(5).Value = Me.NazwiskoTextBox.Text.ToString
Form4.DbTableDataGridView.CurrentRow.Cells(4).Value = Me.ImiéTextBox.Text.ToString
It works fine.
Cheers

VB.Net Add value from datagridview to listbox in another form

I have a trouble on VB.Net desktop programming, I have a Form with a ListBox, Called Form1, this form have a button that will appear another form with DataGridView inside that new Form, we will call this as Participant. Then I have a scenario, when User click the Button, it will call the Participant Form, and when user click the datagridview row in the Participant Form, it will get the value from that row and add the value to the ListBox in Form1, I have done getting the value from the row, but I can't add the value to the ListBox. This is my code to help you understand what I have done.
This is the code that will call the Participant Form :
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Dim myForm As New Participant
If myForm Is Nothing Then
myForm = New Participant
End If
myForm.Show()
End Sub
And this is the code that will add the value from DataGridView in Participant Form to the ListBox in Form1
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 1
If e.ColumnIndex = yourColumnIndex Then
participant_Share = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Form1.ListBox1.Items.Add(participant_Share)
End If
End Sub
But this code cannot insert the value to the ListBox, Please help me if you know how to solve this.
Thank you
You can change Participant constructor to accept a ListBox as parameter and store it in a local variable.
Private yourListBoxReference As ListBox
Public Sub New(ByVal yourListBox As ListBox)
InitializeComponent()
yourListBoxReference = yourListBox
End Sub
So you can call this form as:
myForm = New Participant(yourListBox)
And use the local variable to add an element to your ListBox:
yourListBoxReference.Items.Add(participant_Share)

Label won't update from separate form

I have 2 forms in my program. Form1 has a tab control on it, and on one of these tabs there are a load of labels. Form2 has a few textboxes and dropdown lists on it. Form1 has a button on it that opens Form2 on top of it as a normal form, not as a dialog.
There is code in Form1 that on loading populates the labels on it from a MySQL database. This code is in a separate public sub in the form that is called when the form loads.
What I am trying to do is fill in some of the boxes on Form2, when this Form closes it updates the database with these values (Works fine) and then those values are displayed on Form1. I can get Form2 to run the code in Form1 that populates the labels, but the problem is that the labels on Form1 never actually change. I have tried multiple things, including calling refresh on the labels, trying to change the labels from within Form2 instead of Form1, but they just never update. The .text value of the labels is being updated to the new value, I have checked this via debugging and stopping at the correct points to see what the value is.
Any ideas anyone?? I think it might be to do with the fact that it's Form2 calling the code and for some reason it doesn't have access to change the labels on Form1.
Code:
Form1 (AECSurveyForm)
Private Sub AECSurvey_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadMeterData()
End Sub
Public Sub LoadMeterData()
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
'utility
Meter1UtilityLabel.Text = AECMeteringData(0)("Utility1")
End Sub
Private Sub MeterButton1_Click(sender As Object, e As EventArgs) Handles Meter1Button.Click
If Not Application.OpenForms().OfType(Of AECMeteringDataForm).Any Then
AECMeteringDataForm.GetData(AECGlobalValues.CurrentSurveyUniqueIdentifier, 1)
AECMeteringDataForm.Show()
End If
End Sub
Form2 (AECMeteringDataForm)
Private Sub AECMeteringDataForm_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Closing
Dim AECMeteringDataAdapter As New AECMeteringDataTableAdapter
Dim AECMeteringData As AECMeteringDataDataTable
AECMeteringData = AECMeteringDataAdapter.GetAECMeterDataBySurveyUniqueIdentifier(AECGlobalValues.CurrentSurveyUniqueIdentifier)
AECMeteringData(0)("Utility1") = UtilityComboBox.SelectedItem.ToString
AECMeteringDataAdapter.Update(AECMeteringData)
AECSurveyForm.LoadMeterData()
End Sub

Passing variables between windows forms in VS 2010

I have two forms. Form 1 allows the user to pick an employee from a dropdown combo box. That employee is then passed onto Form 2 where the user enters additional information regarding that employee. That data will then be passed into a SQL table.
On form 1 I have:
Dim ChangeJobInfo As New Form2(Employee)
ChangeJobInfo.Show()
On Form 2 I have:
Public Sub New(ByVal Employee As String)
MsgBox(Employee)
End Sub
The variable passes just fine. The issue is that nothing shows up on the new form. When I setup Form2, I added a combobox, date picker, two text boxes, submit button, etc., but when the form loads it is completely blank. No errors, the MsgBox returns the right result, but none of my gui elements show up. If I change the code on form 1 to Form2.show() I see the form as laid out in the designer.
Any ideas on how to get those items to show up?
Change your code in Form2.vb for the New sub to this:
Public Sub New(ByVal Employee As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
MsgBox(Employee)
End Sub
If you don't call InitializeComponent(), your complete GUI is not going to render.
You don't even have to use the InitializeComponent or New functions.
I have made an example to show how easily this can be done.
Clicking "Show Form" results in the below:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
which is simply used to display the second form.
By clicking "Pass Data" results in the following code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Form2.Label1.Text = TextBox1.Text
End Sub
As shown above you can pass the data directly from control to control. The same idea can be used with variables too.
I am late but I think this answer can help.
For example, Form1 named "menu" opens and passes variable to Form2 named "ordine".
The variable to pass is "hotel"
In menu on button_click
Dim ordinef As New Ordine()
If ordinef Is Nothing Then
'control that form is not opened yet if open close before
ordinef = New Ordine()
Else
ordinef.Close()
ordinef = New Ordine()
End If
ordinef.hotel = hotel
ordinef.Show()
In Form2 (Ordine):
Private Sub Ordine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
public hotel as string
msgbox hotel
That's it!!