Having trouble getting data to show correctly in ComboBox - vb.net

dbConn.Open()
Dim Query1 As String = cmbManufacturer.Text
Dim AccessDataAdapter = New System.Data.OleDb.OleDbDataAdapter
AccessDataAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand("Select Models.[ModelName] From Models Where Manufacturer = '" & Query1 & "'", dbConn)
Dim AccessDataset = New DataSet
AccessDataAdapter.Fill(AccessDataset, "Query1")
cmbModel.DataSource = AccessDataset.Tables("Query1")
dbConn.Close()
So what this does is, it takes the selection from the manufacturer combo-box and determines the values that will display in the model combo-box
I've seen similar questions like this, but mostly in c# so I'm having trouble relating them. The problem is the output on the desired combo-box is
System.Data.DataRowView
when I need it to be the model names that it's pulling from the query. I changed this from a set of static entries in each combo box to make them dynamic, so in the future we wouldn't need to change the code to add or remove devices from the program.

When .DataSource is using for filling ComboBox with items, then DisplayMember used to retrieve a dipslayed text from the bounded object
From MSDN:
If the specified property does not exist on the object or the value of
DisplayMember is an empty string (""), the results of the object's
ToString method are displayed instead.
You are not used DisplayMember property, so method ToString() was called on the object, which is System.Data.DataRowView
You need to set a column name which you want use as displayed text
cmbModel.DisplayMember= "ModelName"
'If you will use a SelectedValue for furthers purposes
'Then you maybe want to set a ValueMember too
cmbModel.ValueMember= "ModelName"
cmbModel.DataSource = AccessDataset.Tables("Query1")
DisplayMember Property

Related

TableAdapter data has changed but property "has Changes" stays at "false" so udate does nothing

It's mostly in the title...
I'm using VB (obviously, see below), but I'm a total beginner with visual studio.
Here is the test code I'm using (it is a simple test button I designed to test the problem I have in the code elsewhere):
Private Sub Test_Click(sender As Object, e As EventArgs) Handles Test.Click
Dim FDBdataset As New FDBDataSet()
Dim FDBTableAdapter As New FDBDataSetTableAdapters.T_PicturesTableAdapter
For Each row As DataRow In FDBTableAdapter.GetData()
row.BeginEdit()
If row("id").ToString = 58672.ToString Then
row.BeginEdit()
Console.Write("Previous Value = " & row("ImgFileName").ToString)
row("ImgFileName") = "Tagada"
row.EndEdit()
Console.WriteLine(", Current Row Value = " & row("ImgFileName").ToString & " - HasChanges : " & FDBdataset.HasChanges())
End If
Next
FDBTableAdapter.Update(FDBdataset)
The output in the console is:
Previous Value = Aphidecta_obliterata_58672, Current Row Value = Tagada - HasChanges : False
I don't understand what is wrong and how to correct it...
I would be very grateful for some help here !
TableAdapter seems set up correctly, and I can read from it, parse rows; display field values, etc...
Update method reports as being correctly set up by the datasource designer.
Code runs without errors but does not affect the DB content.
Why would you think that FDBdataset has any changes in it? Where are you making any changes to it? You have this:
For Each row As DataRow In FDBTableAdapter.GetData()
and GetData returns a new DataTable, so you're making change to that DataTable completely independent of FDBdataset. Use one or the other but not both.
Based on the code you have there, you don't need the DataSet. You can just use the DataTable returnd by GetData:
Dim adapter As New FDBDataSetTableAdapters.T_PicturesTableAdapter
Dim table = FDBTableAdapter.GetData()
For Each row In table
If row.id = 58672 Then
row.ImgFileName = "Tagada"
End If
Next
adapter.Update(table)
Notice that I tidied up your code a lot too. If you're going to use a typed DataSet then use it.
If you actually do need a DataSet for some reason then make the changes to it, not an unrelated DataTable:
Dim data As New FDBDataSet
Dim adapter As New FDBDataSetTableAdapters.T_PicturesTableAdapter
adapter.Fill(data)
For Each row In data.T_Pictures
If row.id = 58672 Then
row.ImgFileName = "Tagada"
End If
Next
adapter.Update(data)
That second code snippet may need some adjustments but I think it should work.
So, the moral of the story is that the Fill method populates an existing DataTable, which can be part of a DataSet but doesn't have to be, while the GetData method creates a new DataTable, populates it and returns it. That new DataTable is not part of any DataSet.

ComboBox shows the first item when loaded into new form VB.Net

I have two forms which are connected, the first one has a datagridview(DGV) that will show list of staff.
When I double click the row the data from the FGV will transfer into second form for user to edit the data. Each column from the DGV will insert at textbox combobox etc.
But the combobox on second form are using database when I double click the DGV; the others is fine but for combobox it will show the first data in database.
This is the code for the double click on the first form:
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim frmEdit As New Edit
frmEdit.lblEENo.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
frmEdit.txtName.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString
frmEdit.txtAge.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString
frmEdit.lblAgeCategory.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString
frmEdit.txtGender.Text = DataGridView1.CurrentRow.Cells(5).Value.ToString
frmEdit.cbEthnicity.Text = DataGridView1.CurrentRow.Cells(6).Value.ToString
frmEdit.cbGrade.Text = DataGridView1.CurrentRow.Cells(7).Value.ToString
frmEdit.cbCategory.Text = DataGridView1.CurrentRow.Cells(8).Value.ToString
frmEdit.cbDepartment.Text = DataGridView1.CurrentRow.Cells(9).Value.ToString
frmEdit.cbPosition.Text = DataGridView1.CurrentRow.Cells(10).Value.ToString
frmEdit.txtReporting.Text = DataGridView1.CurrentRow.Cells(11).Value.ToString
frmEdit.DateTimePicker1.Value = DataGridView1.CurrentRow.Cells(12).Value.ToString
frmEdit.DateTimePicker2.Value = DataGridView1.CurrentRow.Cells(13).Value.ToString
If DataGridView1.CurrentRow.Cells(14).Value.ToString = "Y" Then
frmEdit.rbYes.Checked = True
ElseIf DataGridView1.CurrentRow.Cells(14).Value.ToString = "N" Then
frmEdit.rbNo.Checked = True
End If
frmEdit.cbStatus.Text = DataGridView1.CurrentRow.Cells(15).Value.ToString
frmEdit.txtNote.Text = DataGridView1.CurrentRow.Cells(16).Value.ToString
frmEdit.lblMody.Text = tslblLoginAs.Text
frmEdit.ShowDialog()
load_data()
End Sub
This is second form's code for connecting to the database table for the combobox:
Private Sub Edit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim frmMasterList As New MasterStaff
Dim poscmd As New SqlCommand("SELECT * from MasterPositionList", conn)
Dim posadapt As New SqlDataAdapter(poscmd)
Dim postable As New DataTable
posadapt.Fill(postable)
cbPosition.DataSource = postable
cbPosition.DisplayMember = "PositionName"
Dim depcmd As New SqlCommand("SELECT * from MasterDepartmentList", conn)
Dim depadapt As New SqlDataAdapter(depcmd)
Dim deptable As New DataTable
depadapt.Fill(deptable)
cbDepartment.DataSource = deptable
cbDepartment.DisplayMember = "DeparmentName"
End Sub
The form load event fires too late to populate the combobox. The event is raised almost immediately when you create the form instance in the DataGridView1_CellDoubleClick() method, but the event handler can't actually run until control is returned to the messaging loop, and there's no chance for that to happen until the frmEdit.ShowDialog() line, which is after you have set the combobox values. Therefore the Load event runs after you have populated your desired value and will overwrite that work.
To fix this, move that Load code to the constructor, just after the InitializeComponent() method.
Additionally, since you want to select from the items provided by the database you should look at using the SelectedItem or SelectedValue property, instead of the Text property.
Finally, it's really poor practice to reuse the same connection object throughout an application or form. Yes, database connections are expensive objects to create and manage, but the ADO.Net library already handles this for you. The SqlConnection object you work with in code is a light-weight wrapper over a pool of the more-expensive actual raw connections. When you try to reuse one SqlConnection, you gain efficiency in the cheap thing while losing efficiency in the more expensive thing.
Instead, create (and promptly Dispose()!) a new SqlConnection object every time you need to use the database, and think about reducing connections by reducing round-trips to the database. In this case, you can combine the two SELECT statements into a single string and fill two DataTables in the same database trip:
Private Sub New ' Replaces the current Edit_Load() method
InitializeComponent()
Dim results As New DataSet
'Using block will make sure the connection is closed, **even if an exception is thrown**
' Do NOT(!) try to reuse the connection. Only reuse the connection string.
Using conn As New SqlConnection(connString), _
' Two SELECT queries in one string
cmd As New SqlCommand("SELECT * from MasterPositionList;SELECT * from MasterDepartmentList", conn), _
da As New DataAdapter(cmd)
da.Fill(results)
End Using 'Dispose the database objects as quickly as possible
cbPosition.DisplayMember = "PositionName"
cbDepartment.DisplayMember = "DeparmentName"
cbPosition.DataSource = results.Tables(0) 'Filled two tables in one database call
cbDepartment.DataSource = results.Tables(1)
End Sub
I am wondering why you need a separate form when you could edit the data directly in the DGV. When you think of it, all the data you're trying to edit is already found in the DGV (or should exist in its datasource). You can even have combo boxes inside the DGV, with their own bound datatables. Here is an example that shows how it could be done.
In Edit_Load there is unnecessary repetition. You load the same dataset twice. You could reuse it or even load it at startup and keep it in cache if it's not changing during application runtime.
Plus, rather than fetch values from the DGV, it would better to fetch the selected datarow instead. For this you just need the record ID...
There is no need to access UI attributes, use the underlying datatable instead.
You shouldn't be using index numbers. Imagine the mess and potential for bugs if you want to insert columns or move columns. Also, the user probably can reorder columns in the DGV by drag & drop... thus your logic is ruined and the program will not behave like it should.
Anyway, to answer your issue: you've loaded a datatable, now all you have to do is bind it to the combo (which you did). You have used the DataSource and DisplayMember attributes. You could use SelectedValue to preselect a value in the list like this:
With cbPosition
.DisplayMember = "PositionName"
.ValueMember = "PositionName"
.SelectedValue = "The text value that comes from the DGV"
.DataSource = postable
End With
As long as the value from the DGV is present in the datatable this should work.
Again, I think you should simplify your UI. All the data could be edited in-place in the DGV.

Is there an easy VBA code using Split() Function to get a list of values for a combo box that was originally from a text box as a string?

On my access form, I have a text box that will be a string of characters with multiple "/"s throughout the string. I want to use the split function to separate this string into a list of values to use for my combo box on a subform.
I know it's somewhere along the lines of:
Public Function MakeList()
Dim MyList as String
Dim txt as String
txt = [myTextBoxField].Value
MyList = Split(txt,"/")
Either:
[myComboBox].Value = MyList
Or:
[myTextBoxField].Value = MyList
End Sub
I am not sure if this is supposed to be on "Form Load" or in a module for the Public Function.
All other code shows a For Loop or Debug.Print. I am looking to store this list as a field in my table and then use that field for my Row Source in my combo box.
First, combobox RowSourceType property must be set to ValueList. Next, VBA sets RowSource property, not Value. List is not a property of combobox in Access. Simply:
Me.myComboBox.RowSource = Replace(Me.myTextBoxField, "/", ";")
Form Load event should be appropriate.

combobox default value, vb.net

i have a combobox that populates from a database column but when the form loads, the combobox appears with the first item on the database. the value of this particular combobox determines the value of some other controls on the form so i want the combobox to load with an empty entry. thanks as i'll appreciate any help i can get.
here is my code:
'all variables declared...
connection.Open()
command = New MySqlCommand("SELECT PUMP FROM test.pump", connection)
dataadapter.SelectCommand = command
dataadapter.Fill(dataset)
With Me.PumpComboBox
.DisplayMember = "pump"
.DataSource = dataset
End With
Use
combobox1.SelectedIndex = -1
This sets the value of the combo box to a blank entry.
Maybe adding empty value and then selected that empty value using selectedindex after the combbobox populated will work
In your Form.Load include the following:
ComboBox1.SelectedIndex = 1
Here "ComboBox1" is your ComboBox and "1" equals index you want to be set by default.

How to set ComboBox/RepositoryItemComboBox datasource from a DataTable/TableAdapter?

I am wondering how can I set datasource to a ComboBox in case I want to load data from database everytime my grid loads up.
This is what I did for now (I hardcoded 3 items just to see is it working)
Dim riCombo As RepositoryItemComboBox = New RepositoryItemComboBox
riCombo.Items.AddRange(New String() {"London", "Berlin", "Paris"})
gridArticles.RepositoryItems.Add(riCombo)
colArticles.ColumnEdit = riCombo
I am getting data from database throught DataTableAdapter, there I am writing methods which are triggering sql queries.
After DIMITRY suggestions:
Dim riLookup As New RepositoryItemLookUpEdit()
riLookup.DataSource = DataTableDobTableAdapter.FillDob(Me.DsOrders.DataTableDob)
riLookup.ValueMember = "ID"
riLookup.DisplayMember = "TITLE"
riLookup.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup
GridView1.Columns("CODE").ColumnEdit = riLookup
GridView1.BestFitColumns()
But what I am getting here for column where valuesin dropdowns should be is next:
Thanks guys
Cheers
To fill the ComboBoxEdit editor's items, you can load data from your database to a list, then traverse all items in the list and add them to the Items collection. For example:
For Each row As DataRow In dataTable
Dim cellData As String = DirectCast(row(columnName), String)
riCombo.Items.Add(cellData)
Next
Also, you can use one of available Lookup Editors. These editors provide the DataSource property allowing you to load data from the data base to a list, and then assign this list to the DataSource property. Once this is done, simply set the KeyMember and ValueMember properties. This way, you don't need to fill items manually.