DataGridView with dropdown column - vb.net

I got back to VB.NET after being some years abstinent to VB. Offcourse you can do everything with the designer, but.
I want to implement database functionalities by code. I have a DataGridView which will be filled like I expect. Changes will be safed, ok for now.
But I'd like to change the field which represents an foreignkey value to a dropdown field. I tried by myself and searched for a good solution, but nothing found.
This is part of my code:
.AutoGenerateColumns = True
.DataSource = TMitarbeiterBindingSource
.AutoSizeRowsMode = _
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
There a six columns and I want the third to be a dropdown field which should use another BindingSource. Propably I have to change AutoGenerateColumns to false, but then how to add the columns manually. And after that I have to change one of the columns, but how?
If anybody would have an example that fits with my code would be helpful.

I got it, I used the following code and it works:
With rufnummer
.DataPropertyName = "Rufnummer"
.DataSource = db.TGeraeteBindingSource
.DisplayMember = "Rufnummer"
.ValueMember = "Rufnummer"
.FlatStyle = System.Windows.Forms.FlatStyle.Flat
.HeaderText = "Rufnummer"
.Width = 70
End With

Related

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.

Datasource for every control in GroupBox

Is something as this even possible. I'm pasting code bellow and hopping anyone could show me the right way to do it.
For Each tbbox As TableLayoutPanel In GroupBox3.Controls
'looping through all controls in my tablelayoutpanle
For Each ctl As Control In tbbox.Controls
If ctl.Name.StartsWith("cb_barva") Then
'im stuck here...
With (ctl)
.DataSource = ds_barve.Tables("moje_barve")
.DisplayMember = "barva"
.ValueMember = "barva"
.SelectedIndex = 0
End With
End If
Next
Next
You need the type conversion
With (ctl)
Convert the ctl to ComboBox
ctype(ctl,ComboBox)
If your not able to convert the control with "With" statement then change each line of your code like below....
ctype(ctl,ComboBox).DataSource = ds_barve.Tables("moje_barve")
ctype(ctl,ComboBox).DisplayMember = "barva"
ctype(ctl,ComboBox).ValueMember = "barva"
ctype(ctl,ComboBox).SelectedIndex = 0

vb2010 combobox automatic change text from items

I am using vb2010 and I have a problem with combobox. My code below get the items from mysql database then add it to the combobox. When there is an item in combobox say for example "NERISON" when I input "N" in the combox and press tab, the combobox will automatically change the text to "NERISON" -what I don't want. I just want to leave it with "N" as text. How would I do that?
If Not e.KeyChar = ChrW(8) Then
txtprice.Text = ""
With cmb_particular
.Items.Clear()
load_dbase() ' connects to database
CNN.Open()
runSql("select particular from particular where status=0 and particular like '%" & .Text & "%' order by particular") ' my function for queries
While dr.Read
.Items.Add(dr("particular"))
End While
CNN.Close()
.SelectionStart = cmb_particular.Text.Length
.DroppedDown = True
End With
End If
Could it be that autofill / autocompelte is enabled in the combobox?
For WPF, set the following:
IsTextSearchEnabled = False
For Forms:
ComboBox.AutoCompleteMode = False
In your combobox properties be sure that AutoCompleteMode=None
#Nerison:
I 've added a comboBox in a form. I don't change anything. I check its properties:
ComboBox1.AutoCompleteMode=None
ComboBox1.AutoCompleteSource=None
ComboBox1.DropDownStyle=DropDown
I add a datatable as datasource. Now I have the bahaviour that you want. I type "N" and it doesn't suggest or append anything.
Could you please check it again?

combobox with first row blank

I have a a ComboBox bound to a DataSet. I would like to have combobox with very first row blank. How can I do this?
I've tried following
With .RoomComboBox
.DataSource = Me.aRoomsBindingSourse
.DisplayMember = "Room"
.ValueMember = "BedCode"
.DataBindings.Add("text", aRoomsBindingSourse, "Room", False,DataSourceUpdateMode.Never)
.SelectedIndex = -1
End With
Thank you in advance.
I don't think you can do this. Because .net framework doesn't allow to modify items if the DataSource set.
Hook into the post data bind event and add a row at the first location.

vb.net. How do I bind dataset to DataRepeater?

I am looking for a vb.net example of how to bind a dataset/datatable to data repeater and have the data elements bound to the columns of the dataset/datatable?
Thanks
At first I thought you wanted a web repeater, but from your comments I realized you meant Microsoft.VisualBasic.PowerPacks.DataRepeater.
I need some more info from you to give the most helpful sample code (see below).
The basic steps of using a DataRepeater are:
1) Install the Visual Basic Power Packs 3 Link
2) Open a VB.net Winforms project and drag a DataRepeater to your form
3) Add a new Dataset to your project via Add->New Item menu
4) In the design window, set up columns as desired
5) Open the Data Sources window from Data->ShowDataSources menu
6) With your form in design mode, go to the Dataset in the Data Sources window and use the dropdown box next to the table name to select "Details"
7) Drag the table to top section of the DataRepeater control (on your form). The table fields should now be listed on your DataRepeater. You can move them around.
8) At runtime, you can load the Datset and the DataRepeater will automatically reflect the data changes. No .Databind is required
ex.
me.DataSet1.Tables(0).Columns.Add(New String() {"John", "Doe", "Accountant"}
or
myDataAdapter.Fill(me.DataSet1.Tables(0))
Do you get tripped up on any of those steps?
Edit:
From what I have seen, it looks like DataRepeater is meant to be used in situations were you add/map the datatable to the DataRepeater at design-time. Then all you have to do is fill the datatable at run-time and the DataReader shows the data automatically.
If you really want to add the table/controls to the DataRepeater at run time, here's an example I coded for you. It assumes a form named Form3 with a DataRepeater named DataRepeater1 and a button named Button1...
Public Class Form3
''Set up demo DataSet/DataTable
Const FRUIT_COL As String = "Fruit"
Const COLOR_COL As String = "Color"
MyDataSet = New DataSet
MyDataSet.Tables.Add("MyTable")
With MyDataSet.Tables(0)
.Columns.Add(FRUIT_COL, GetType(System.String))
.Columns.Add(COLOR_COL, GetType(System.String))
End With
''Populate the DataTable with sample data. You would be loading from SQL
With MyDataSet.Tables(0)
.Rows.Add(New String() {"Apple", "Red"})
.Rows.Add(New String() {"Orange", "Orange"})
.Rows.Add(New String() {"Banana", "Yellow"})
End With
''These objects would normally be created automatically if you added DataTable to DataRepeater at design-time
FruitLabel = New Label
FruitTextBox = New TextBox
ColorLabel = New Label
ColorTextBox = New TextBox
With FruitLabel
.AutoSize = True
.Location = New Point(10, 20)
.Name = "FruitLabel"
.Text = FRUIT_COL
End With
With ColorLabel
.AutoSize = True
.Location = New Point(10, 60)
.Name = "FruitLabel"
.Text = FRUIT_COL
End With
With FruitTextBox
.Location = New Point(50, 20)
.Size = New Size(60, 15)
.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyDataSet.Tables(0), FRUIT_COL, True))
End With
With ColorTextBox
.Size = New Size(60, 15)
.Location = New Point(50, 60)
.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.MyDataSet.Tables(0), COLOR_COL, True))
End With
''Add the controls that will be displayed for each row in DataTable
With DataRepeater1
.ItemTemplate.Controls.Add(FruitLabel)
.ItemTemplate.Controls.Add(FruitTextBox)
.ItemTemplate.Controls.Add(ColorLabel)
.ItemTemplate.Controls.Add(ColorTextBox)
End With
''Run-time population of DataRepeater from your DataTable
DataRepeater1.DataSource = MyDataSet
DataRepeater1.DataMember = MyDataSet.Tables(0).TableName
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Example of how you can add additional rows with form elements
With MyDataSet.Tables(0)
.Rows.Add(New String() {"Grapes", "Green"})
End With
End Sub
End Class
Now that you have seen the code, I hope you don't use it :)
I suggest you either set up your DataSet structure at design-time or use a different control to display your data.
Here's a link that has lots of information about the typical way to use DataRepeater:
Link
Final Edit
The user's last bug was a case-sensitivity issue. The string "Text" in the following line of code must be capitalized to match the control's property name. I recommend creating a const for "Test" to avoid this typo.
[ControlName].DataBindings.Add(New System.Windows.Forms.Binding("Text", [DataTable], [Column Name], True))
Dim data As DataSet
DataRepeater1.DataSource = data
DataRepeater1.DataBind()