Display ValueMember when selection is changed - vb.net

I have searched all day for what I would think would be a simple concept, but that is what I get for thinking.
I have a DataGridView control with a ComboBox column that uses a DataTable as its DataSource as follows:
With CType(dgvICS213.Columns(GridCol("CostBasis").Name), DataGridViewComboBoxColumn)
.DataSource = Common.ExecuteQuery("SELECT ID, CostBasis FROM ICS213CostBasis", CommandType.Text)
.DisplayMember = "CostBasis"
.ValueMember = "ID"
End With
The database table stores the value (ID) from the combo box.
I use the DefaultValuesNeeded event to set new values to the DisplayMember (CostBasis) value:
Dim dgv As DataGridView = sender
CType(e.Row.Cells(GridCol("CostBasis").Index), DataGridViewComboBoxCell).Value = CType(dgv.Columns(GridCol("CostBasis").Index), DataGridViewComboBoxColumn).Items(0).Item(1)
I also use the DisplayMember value to fill existing rows from a database query (handling the DataError event)
So far, so good but when I select a an entry from the combox, the ValueMember is displayed in the combo instead of the DisplayMember. Can someone explain where I am going wrong.

I faced the same problem and solved it by changing the field in the database from byte to int16.

Related

fill textbox based on combobox selection vb.net *data not bound [duplicate]

I got a bounded combobox to a group name in vb.net. i can view in the drop down items of the combobox the set of GROUP NAMES. but when i do an insert or update query, i need to make the selected group name refers to the GROUP NUMBER. I don't want to store letters in the database, instead i prefer numbers. how can i do that?!
Here is my code so far :
cmd1.Parameters.AddWithValue("#group", DirectCast(Additemcombobox.SelectedItem,
DataRowView).Item("GroupName"))
Storing the group name in database is currently working well.
My question might not be well explained. Please ask me in case...
any help would be appreciated
You can show one element to the user such as the name, but use a different one for the code to identify the item using DisplayMember and ValueMember
Dim SQL = "SELECT Id, Name FROM GroupCode ORDER BY Name"
...
GrpDT = New DataTable
GrpDT.Load(cmd.ExecuteReader)
cboGroup.DataSource = GrpDT
cboGroup.DisplayMember = "Name"
cboGroup.ValueMember = "Id"
The user will only see the names, while the code can use ValueMember:
Private Sub cboGroup_SelectedValueChanged(...etc
Console.WriteLine(cboGroup.SelectedValue)
End Sub
It prints the Group ID not the name. Depending on the ORDER BY clause in the SQL and the ID, the SelectedIndex may or may not match, so respond to SelectedValueChanged event. If you use SelectedValue instead of SelectedItem you wont have to thrash about with a DataRowView item.
Note that SelectedValue is Object so you will have to cast to integer or whatever for use elsewhere.

Query Datagridview and output to Datagridview vb.net

I just want to make sure I am reading microsoft's documentation correctly, and that I understand the structure appropriately.
First here is link I am reading:
https://msdn.microsoft.com/en-us/library/bb669099(v=vs.110).aspx
Second here is the code they suggested:
' Create a table from the bound view representing a query of
' available products.
Dim view As DataView = CType(bindingSource1.DataSource, DataView)
Dim productsTable As DataTable = CType(view.Table, DataTable)
' Set RowStateFilter to display the current rows.
view.RowStateFilter = DataViewRowState.CurrentRows
' Query the DataView for red colored products ordered by list price.
Dim productQuery = From rowView As DataRowView In view _
Where rowView.Row.Field(Of String)("Color") = "Red" _
Order By rowView.Row.Field(Of Decimal)("ListPrice") _
Select New With {.Name = rowView.Row.Field(Of String)("Name"), _
.Color = rowView.Row.Field(Of String)("Color"), _
.Price = rowView.Row.Field(Of Decimal)("ListPrice")}
' Bind the query results to another DataGridView.
dataGridView2.DataSource = productQuery.ToList()
Sorry if this seems like a dumb question, but my inquiry is mainly related to understanding how to query it appropriately. The "SELECT" part of this code is at the end, correct? (i.e. Select New With, column_name, column_name etc etc). Sorry to put it in SQL terms but I was wondering if it was more efficient to query a datagridview (kind of using it like a temporary table for a query) than having to either reach back out to DB or loop through contents to check for so and so variable.
And is "bindingSource1" the first datagridview, or the source data (table in db)?

VB.Net Fill List with SQL Query

I'm trying to fill either a combobox or a list with an sql query, I can get them to produce the number of entries pulled but not the names of the entries, and not multiple entries.
The code in question is simple:
Dim RegisterApt As New StudentsDataSetTableAdapters.TestTableAdapter
Try
txtTestPull.Items.Add(RegisterApt.FillByStudentsTest(StudentsDataSet.Test, StudentInsert.School, StudentInsert.School))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What I can't seem to find online is how to do this.
All I'd like to do is pull results using my sql query which I know works,
and push the resulting rows to the list or combobox
Here's the step by step on how to fill a ComboBox and a DataGridView using ADO.Net. I use the Northwind database as a sample.
1. Add a DataSet
Right-click your project and choose Add, then choose New Item. Choose Dataset in the next window.
2. Add a DataTable
Connect to your database and drag a table into the middle area. In this example, I choose the Customers table.
3. Add a query
Right-click your DataTable, and choose Add, then choose Query.
Choose Use SQL statements in the next window, click Next.
Choose SELECT which returns rows in the next window, click Next.
4. Write a query
SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax
FROM dbo.Customers
WHERE Country = #Country
I add a WHERE clause to filter the data. Click Next.
Gives names to your methods. I use FillByCountry in the first textbox and GetDataByCountry in the second textbox.
Save your project and build it first because you're adding a new DataSet.
5. Add some controls in your form
Add a Button, a TextBox, a ComboBox and a DataGridView. You can change the names, but I use the default names in this example.
6. Write some code to get the data and bind it to ComboBox and DataGridView
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' declare a DataTable
Dim dt As New DataSet1.CustomersDataTable
' declare a DataAdapter
Dim da As New DataSet1TableAdapters.CustomersTableAdapter
' use the DataAdapter to fill the DataTable
da.FillByCountry(dt, TextBox1.Text)
' bind the DataTable to a DataGridView
DataGridView1.DataSource = dt
' bind the DataTable to a ComboBox
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "CustomerID"
ComboBox1.DisplayMember = "CompanyName"
End Sub
End Class
7. Run the project and see the result

Filtering a DataGridView Twice

I have a DataGridView with a bindingsource from an Access table. I am using this code:
QrysbfrmPupilsAndExamsBindingSource.Filter = "CandNumber = " & TextBox1.Text
to filter the datagridview by the column CandNumber. I then want to filter it again, keeping the results from the previous filter, not filtering from the full access table again.
When you bind a DataTable to a BindingSource, you can use an expressions using the syntax described within the DataColumn.Expression property.
So if you e.g. want to filter by an additional column, just use And:
yourBindingSource.Filter = String.Format("CandNumber = {0} AND Something = {1}",
TextBox1.Text, TextBox2.Text)

Linking result of a query that related to a Listbox value to textboxes?

I have a problem in MS Access and I don't know how can I solve this problem.
I have a listbox on my Form. If the use changes the value of the Listbox,a SQL query should be run and the result should be displayed is some Textboxes. For example:
The user select 'A' value From the list box then this query should be run:
SELECT XValue, YValue,Wert FROM Mytable WHERE Name='A' and ID=fzgID
then I want to display XValue, YValue and Wert in three textboxes. The problem is may be I have more than one pair for this combination (XValue, YValue,Wert).
How can I do that? How can I Link the list box and the query to the textboxes?
thank you so much
Every ListBox has a ListBox_Click() procedure, which is run when an element of the listbox is clicked.
You can get, what you want, if you do something like this:
Sub ListBox1_Click()
Dim valueOfListbox As String
valueOfListBox = ListBox1.Value
' **** here comes your code ****
' get the actual value from the listbox and do the query
' change the values of the textboxes to the result of the query
End Sub
If your listbox is named different than "ListBox1" you have to change the name of the procedure. For example if the listbox is named "blaBox" it has to be blaBox_Click().
Depending on the structure of your data, I would do something like this. Set the table/query of your listbox to have all the data you want to display.
SELECT ID, Name, XValue, YValue, Wert FROM Mytable
In the Format tab of the property sheet of the listbox, set the column count to 2, and the column widths to 0";2" or your preference. This will hide the other values in the listbox rows.
In the three textboxes, set the control source like so.
Textbox1.ControlSource: =[Listbox1].[Columns](3)
Textbox2.ControlSource: =[Listbox1].[Columns](4)
Textbox3.ControlSource: =[Listbox1].[Columns](5)
You may need to adjust the numbers. A listbox has a property called Columns that enable you to access the values at different columns of the listbox. If you don't want to or cannot have all the data in the listbox, then you can use the DLookUp function in each textbox.
Textbox1.ControlSource: =DLookUp("XValue", "Mytable", "ID=""fzGID"" And Name=""" & [Listbox1] & """")
The reference to [Listbox1] will pull the value of the bound column. If the bound column is not the data you are looking up by then you will need to reference a column (i.e. [Listbox1].[Columns](2)).