Referencing cell values on DataGridView from another form - vb.net

This is bugging me. I have a "main" form named frmMain with a DataGridView object named objDataGrid.
When a user double clicks on a row or clicks another button, I hide the main form, open a new form and want to reference the values in the row selected by the user but I keep getting an error when I try to access some, but not all, of the datagridview's properties.
For example here is the code that opens the form:
On Error Resume Next
Me.Hide()
frmGenerate.Show()
In the frmGenerate's load event I do the following:
Dim frmMain As frmMain = Nothing
frmMain = New frmMain
Any time I try to grab a value from the grid I get various errors.
frmMain.objDataGrid.Item(1, 2).Value
frmMain.objDataGrid.Rows(2).Cells(4).Value
frmMain.objDataGrid.SelectedRows(0).Index
frmMain.objDataGrid.Item(1, frmMain.objDataGrid.SelectedRows(0).Index).Value
These return index out of range errors even though I know for sure the indexes are correct.
Any ideas?
Thanks.

Why are you creating a new instance of frmMain? You need to refer to the existing one where they've selected a row.
You could pass just the row of the datagrid or PK field values instead of referencing the other form.

Related

Add item to listbox from another form

I'm trying to add items to listbox from another form but the listbox doesn't seem to update.
I have 3 form : frm1, frm2, frm3
this my code in frm1 to open frm2:
Using frm As New frm2
frm.ShowDialog()
End Using
frm2 has a listbox named list_xxx
code in frm3:
Private Sub add_item()
frm2.list_xxx.add("aaaa")
End Sub
i want to add item to frm2 from frm3. but no success the listbox still empty.
how to fix it?
You are almost certainly referring to the default instance of frm2 in that second code snippet but you aren't displaying the default instance in the first code snippet. If you display one form and then add items to a ListBox on another form, there should be no surprise that you can't see those items. Either use the default instance everywhere or not at all. The more correct option is not at all, but that would require other changes too. The easier option is everywhere, which means changing the first code snippet to this:
frm2.ShowDialog()
No Using statement because you're not creating an object.

Setting ComboBox RowSource property to query in "GotFocus()" method leaves blank values in ComboBox Access VBA

I want to populate my ComboBox in an Access form using VBA based on data from another table. Previously to do this I did the following:
In the Property Sheet -> Data tab I filled out Row Source and Row Source Type fields with this information:
Now whenever I clicked on the dropdown for my combobox, it would populate the dropdown list with all of the names from t_people table.
This limited me however to when data changed in the t_people's name column. In order to get an updated list, I must close the form and re-open it so that the query runs again. I have limited the access to this Access file so that the user is only presented with x number of forms, and cannot close/open them or others.
My solution is to remove the query on the form load, and instead run the query every time the combobox gains focus, has a click event or something of the same sorts. I did this with the following event in VBA:
'Run when the "name" combobox gains focus
Private Sub nameCb_GotFocus()
[nameCb].RowSource = "SELECT name FROM t_people"
End Sub
I have set breakpoints and this code does run. However, the the combobox is not populated after it does. How can I get the combobox to populate with the values from a query for each time the combobox gains focus?
Set the RowSource in design and add a .Requery when entering the control.
Private Sub nameCb_Enter()
nameCb.Requery
End Sub

How can I destroy the previous instance of object and create new one whenever a function is called?

Whenever a user selects a cell in a sheet I want a windows form to appear and any previous windows form that was connected to the previously active cell to be dissapear. How can I do this?
I'm thinking a solution like the following would work, but I don't know if it is good practice and I also don't know how to execute the command that I have commented out
Sub CreateNewFormForActiveCellAndDeleteThePreviousOne()
'Remove all previous instances of class Frm
Dim Frm as New Frm
End Sub
Instead of creating a new form instance I'd suggest updating the data on the form instead. Thus, you will save resources required for creating a new form instance. For example, you may define a public field in the Form's class and update it according to the selected cell in Excel.

Updating a control on another form with the results of a dialog box

I made a windows form which contains a listbox (named VenueList). The listbox is pulling it's values from a list provided by a binding source VenuesBindingSource which is pulling in a ID for the value and a name for the text.
There is a button which is causing a DialogBox to appear which is asking for values to store in the database for a NEW venue. Once the values are filled and insert the database, what's supposed to happen is that the dialog box closes and goes back to the original form which invoked it.
However, instead of updating the list. The list stays the same. But if you close the form and reopen it, you see that a new value was added.
TournamentSettings.VenuesTableAdapter.InsertVenueQuery(Trim(VenueNameTxt.Text), Trim(VenueAddress1Txt.Text), Trim(VenueAddress2Txt.Text), Trim(VenueCityTxt.Text), Trim(VenueProvinceTxt.Text), Trim(VenueZipTxt.Text), Trim(CountryBox.SelectedValue), Trim(VenuePhoneNo.Text), VenueType.SelectedText, VenueWebAddress)
TournamentSettings.VenuesTableAdapter.Fill(TournamentSettings.VenueNameList.Venues)
In the above code, InsertVenueQuery is the name of a query from the designer which is invoked to add the values onto the tableadapter VenuesTableAdapter which is used to fill the combo box on load. I also sent the Fill command to refill the table with the new value.
So the question is, should I go about doing this another way, rather than feeding the Table adapter and sending a fill command on to the datatable? Or is there something that I'm not doing here which I should to force that value into the list. I'm tempted to redo everything outside of the designer but that's a lot of code since I have to essentially run two commands (one to insert the data, and another to get the ##IDENTITY value since this is run on an access database.)
Okay. This one I had to think about for a moment.
Instead of me creating a block of done on the load event, I instead created a sub function called "FillVenueList".
I used the following block of code:
Public Sub FillVenueList()
' Adding values from database to a datatable.
' From there will add to the list box.
Dim VenueConnection As New OleDb.OleDbConnection(DBconnection)
VenueConnection.Open()
Dim VenueConnectionQuery As New OleDb.OleDbCommand("SELECT VenueID, VenueName FROM Venues", VenueConnection)
Dim VenueDataAdapter As New OleDb.OleDbDataAdapter(VenueConnectionQuery)
Dim VenueDataSet As New DataSet
VenueDataAdapter.Fill(VenueDataSet, "Venues")
TrnVenueLst.DataSource = VenueDataSet.Tables("Venues")
TrnVenueLst.DisplayMember = "VenueName"
TrnVenueLst.ValueMember = "VenueID"
VenueConnection.Close()
End Sub
From there I called THIS sub on both the form AND the Add Venue window and I can safely see that this works. SO THAT is how you get a new value onto the form, don't use it as a part of the Load Event but rather call it from the load event block, then call it when you want to add to the list.

Duplicate a DataGridView on a second windows form

I have a bound DGV that took a bit of work to get its columns set up. I'd like to show a 1-row version of this identical DGV on a second windows form. Is there a way to programatically place a copy on the second form. I would adjust the height and position of the 1-row version, and create a new binding source on the second form so that I could filter the data.
MyForm.Controls.Add(myDataGridView)
So further explanation:
In your first for you will need to make a variable or property that contains a reference to the DataGridView that you want to access.
I'd suggest doing something like this.
Public Shared Property myDataGridView As DataGridView
then after you get it set up in the form the way you want it set up
myDataGridView = originalDataGridView
Then in the second form
SecondForm.Controls.Add(FirstForm.myDataGridView)
Will add the DataGridView exactly as it is on the first form.
Edit
If you are creating it in a designer, you can just either copy and past it from the original for to the second form.
Or just on the Form.Shown or in the New() of the first form set the myDataGridView to the DataGridView that you created.