I have a VB.net form and I'm trying to bind some data to controls.
I'm using the following code:
Private Sub InitDataLayer()
'Create table
DataTable = New DataTable
DataTable.Columns.Add("ID")
DataTable.Columns("ID").DataType = GetType(Integer)
DataTable.Columns("ID").AllowDBNull = False
DataTable.Columns.Add("Name")
DataTable.Columns("Name").DataType = GetType(String)
DataTable.Columns("Name").AllowDBNull = False
'Create new rows
'1st Row
Dim NewRow As DataRow = DataTable.NewRow
NewRow.Item("ID") = 1
NewRow.Item("Name") = "John"
DataTable.Rows.Add(NewRow)
'2nd Row
NewRow = DataTable.NewRow
NewRow.Item("ID") = 2
NewRow.Item("Name") = "Steve"
DataTable.Rows.Add(NewRow)
'Bind controls
'Textboxes
TextBoxID.DataBindings.Add(New Binding("text", DataTable, "ID"))
TextBoxName.DataBindings.Add(New Binding("text", DataTable, "Name"))
'Navigator
Dim BS As New BindingSource
BS.DataSource = DataTable
BindingNavigator1.BindingSource = BS
End Sub
The code works but when I move to the second row in the binding navigator, the controls don't update (see Video)
What is missing to the code please ?
Thanks
Solution:
Private Sub InitDataLayer()
'Create table
DataTable = New DataTable
DataTable.Columns.Add("ID")
DataTable.Columns("ID").DataType = GetType(Integer)
DataTable.Columns("ID").AllowDBNull = False
DataTable.Columns.Add("Name")
DataTable.Columns("Name").DataType = GetType(String)
DataTable.Columns("Name").AllowDBNull = False
'Create new rows
Dim NewRow As DataRow = DataTable.NewRow
NewRow.Item("ID") = 1
NewRow.Item("Name") = "John"
DataTable.Rows.Add(NewRow)
NewRow = DataTable.NewRow
NewRow.Item("ID") = 2
NewRow.Item("Name") = "Steve"
DataTable.Rows.Add(NewRow)
'Bind controls
Dim BS As New BindingSource
BS.DataSource = DataTable
TextBoxID.DataBindings.Add(New Binding("text", BS, "ID"))
TextBoxName.DataBindings.Add(New Binding("text", BS, "Name"))
BindingNavigator1.BindingSource = BS
End Sub
Related
I want to display the picture found in the file location that is in the field "Picture" I have this code that shows the fields in the datagridview but I cannot figure how to properly code image from file.
Update - this below code works fine
enter code here
Imports System.Data.OleDb
Public Class test
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter
Private Sub test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
getthedata() ' this does a query and adds a column to the query data datatable
Filldatagrid() ' This fills the datagridview named DG1
End Sub
Sub getthedata()
sel = "select * from dog"
DataConnection()
cmd = New OleDbCommand(sel, con)
ds.Tables.Add(dt)
da = New OleDbDataAdapter(cmd)
da.Fill(dt)
Dim imageColumn As New DataColumn
imageColumn.ColumnName = "Picture"
imageColumn.DataType = GetType(System.Drawing.Image)
dt.Columns.Add(imageColumn)
For Each row As DataRow In dt.Rows
row("Picture") = System.Drawing.Image.FromFile(row("pic"))
Next
dt.AcceptChanges()
con.Close()
End Sub
Sub Filldatagrid()
dg1.AutoGenerateColumns = False
dg1.RowTemplate.Height = 150 ' this is the larger size to accomodate the image
dg1.ColumnCount = 4
dg1.DataSource = dt
dg1.Columns(0).DataPropertyName = "ID"
dg1.Columns(0).Visible = False
dg1.Columns(1).HeaderText = "Name"
dg1.Columns(1).DefaultCellStyle.Font = New Font("Tahoma", 15)
dg1.Columns(1).DefaultCellStyle.ForeColor = Color.DarkGreen
dg1.Columns(1).DataPropertyName = "Nam"
dg1.Columns(1).Width = 100
dg1.Columns(2).HeaderText = "Birthday"
dg1.Columns(2).DefaultCellStyle.Font = New Font("Tahoma", 15)
dg1.Columns(2).DataPropertyName = "Birth"
dg1.Columns(2).Width = 150
dg1.Columns(3).DataPropertyName = "Pic"
dg1.Columns(3).Visible = False
Dim dgvImageColumn As New DataGridViewImageColumn
dgvImageColumn.DataPropertyName = "Picture"
dgvImageColumn.Width = 150
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
dg1.Columns.Add(dgvImageColumn)
End Sub
End Class
The database records are:
Id Nam Birth Pic Comments
1 Brody 5/15/2015 C:\Users\JRS89\Dropbox\Photos\dogs\Brody\Brody.jpg
2 Bella 5/1/2015 C:\Users\JRS89\Dropbox\Photos\dogs\Bella\Bella.jpg
3 Benji 5/15/2016 C:\Users\JRS89\Dropbox\Photos\dogs\Benji\Benji.jpg
Sorry if this is something that should be obvious, but this is my first project using VB.NET, and some things are still beyond me.
I'm trying to setup a combobox inside a DataGridView but I keep getting
System.ArgumentException: DatagridviewComboBoxCell value not valid
I've been googling this for 2 hours now and to me it seems I'm setting up thing correctly, but probably something is amiss.
Dim imageCol As DataGridViewImageColumn
Dim checkCol As DataGridViewCheckBoxColumn
Dim col As DataGridViewColumn
Dim comboCol As DataGridViewComboBoxColumn
Dim ds As DataSet
Dim som As New SomStructure
Dim somministrazioni() As SomStructure = {}
With dgvListaAttivita
.Columns.Clear()
.AutoGenerateColumns = False
.ReadOnly = False
.EditMode = DataGridViewEditMode.EditOnEnter
.CausesValidation = False
somministrazioni.Clear
ds = getSomministrazioni(codevalue, Today())
If ds IsNot Nothing Then
For Each row As DataRow In ds.Tables(0).Rows
som.idOspite = row(0)
som.nomeOspite = row(1)
som.descrizioneSomministrazione = row(2)
som.idOperatore = row(3)
som.nomeOperatore = row(4)
som.preparata = False
somministrazioni.Add(som)
Next
End If
.DataSource = somministrazioni
imageCol = New DataGridViewImageColumn
imageCol.Width = 25
imageCol.ImageLayout = DataGridViewImageCellLayout.Normal
imageCol.Description = "delete"
imageCol.Image = My.Resources.note
.Columns.Add(imageCol)
col = New DataGridViewColumn
col.DataPropertyName = "descrizioneSomministrazione"
col.HeaderText = "Somministrazione"
col.ValueType = GetType(String)
col.CellTemplate = New DataGridViewTextBoxCell
col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
col.ReadOnly = True
.Columns.Add(col)
comboCol = New DataGridViewComboBoxColumn
Dim dt As DataTable = dsOperatori.Tables(0)
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
comboCol.DataPropertyName = "idOperatore"
comboCol.HeaderText = "Operatore"
comboCol.ValueType = GetType(Integer)
comboCol.CellTemplate = New DataGridViewComboBoxCell
comboCol.Width = 150
.Columns.Add(comboCol)
checkCol = New DataGridViewCheckBoxColumn
checkCol.DataPropertyName = "preparata"
checkCol.HeaderText = "P."
checkCol.ValueType = GetType(Boolean)
checkCol.CellTemplate = New DataGridViewCheckBoxCell
checkCol.Width = 20
.Columns.Add(checkCol)
End With
As I understand it it should be like this:
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
specifies the datasource and key/display values of the comboboxes in the column
comboCol.DataPropertyName = "idOperatore"
Is the name of the column linked to both the DataSource of the DataGridView and the ValueMember of the ComboBox, and that should display different selections on different rows.
If I remove the DataPropertyName from the code I don't get the error anymore, but I also get empty comboboxes.
Try commenting out the template line:
comboCol = New DataGridViewComboBoxColumn
Dim dt As DataTable = dsOperatori.Tables(0)
comboCol.DataSource = dt
comboCol.DisplayMember = "display"
comboCol.ValueMember = "idoperatore"
comboCol.DataPropertyName = "idOperatore"
comboCol.HeaderText = "Operatore"
comboCol.ValueType = GetType(Integer)
'comboCol.CellTemplate = New DataGridViewComboBoxCell
comboCol.Width = 150
.Columns.Add(comboCol)
So i have a datagridview on my project , and this is the code of my datagridview
With DataGridView1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.CellBorderStyle = DataGridViewCellBorderStyle.Single
.GridColor = Color.Black
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.AllowUserToResizeColumns = False
.AllowUserToAddRows = False
.ReadOnly = True
.AutoGenerateColumns = False
Me.DataGridView1.Columns("cod_produto").Visible = True
Me.DataGridView1.Columns("nome_produto").Visible = True
Me.DataGridView1.Columns("descricao").Visible = True
Me.DataGridView1.Columns("qtd_existente").Visible = True
Me.DataGridView1.Columns("qtd_minima").Visible = True
Me.DataGridView1.Columns("precounitario_venda").Visible = True
Me.DataGridView1.Columns("nome_fornecedor").Visible = True
Me.DataGridView1.Columns("categoria_produto").Visible = True
end with
Dim listaproduto As List(Of Produto) = CAD.CAD_Listar.ObterProduto()
Me.BindingSource1.DataSource = listaproduto
DataGridView1.DataSource = BindingSource1
now i wanted to create another column containing a combobox wich i did here
Dim lstcategoria As List(Of CategoriaProduto) = CAD.CAD_Listar.ObterCategoria()
Dim comboboxColumn As New DataGridViewComboBoxColumn
With comboboxColumn
.DataSource = lstcategoria
.DisplayMember = "categoriaproduto"
.ValueMember = "cod_categoriaproduto"
End With
DataGridView1.Columns.Add(comboboxColumn)
but this is not working, it does not trow any exeption but the combobox just stays with no items.i know the list is working because if i create a normal combobox(not in a comboboxcolumn) the combobox fills with the values of the list. but for some reason when i try to do the same but in a comboboxcolumn it just does not fill. can someone help me on this? thanks
This code gives me system.data.datarowview show in combobox.But I want to fill the combo box with data. whats the error?
Dim adclasstecher As New SqlDataAdapter
Dim tbclassteacher As New DataTable
If cnSchool.State = ConnectionState.Closed Then cnSchool.Open()
cmclassteacher.Connection = cnSchool
cmclassteacher.CommandText = "SELECT * FROM Teachers"
cmclassteacher.ExecuteNonQuery()
adclasstecher.SelectCommand = cmclassteacher
adclasstecher.Fill(tbclassteacher)
With cmbclzteachr
.DataSource = tbclassteacher
.DisplayMember = "ClassTeacher"
.SelectedIndex = 0
End With
cnSchool.Close()
Dim adclasstecher As New SqlDataAdapter
Dim tbclassteacher As New DataTable
If cnSchool.State = ConnectionState.Closed Then cnSchool.Open()
cmclassteacher.Connection = cnSchool
cmclassteacher.CommandText = "SELECT * FROM Teachers"
cmclassteacher.ExecuteNonQuery()
adclasstecher.SelectCommand = cmclassteacher
adclasstecher.Fill(tbclassteacher)
With cmbclzteachr
.DataSource = tbclassteacher
.DisplayMember = "ClassTeacher"
.SelectedIndex = 0
.databind() //you need to bind the data after providing datasource
End With
cnSchool.Close()
I want to add a item to combobox which already in bounded with some data.
Code:
Public Sub showSection()
sb = New StringBuilder()
sb.Remove(0, sb.Length)
sb.Append("SELECT DISTINCT Section ")
sb.Append(" FROM Employee ")
sb.Append(" ORDER BY Section")
Dim sqlSection As String = sb.ToString()
da = New SqlDataAdapter(sqlSection, Conn)
da.Fill(ds, "Section")
dt = ds.Tables("Section")
bs.DataSource = dt
With cbSection
.DisplayMember = "Section"
.ValueMember = "Section"
.DataSource = ds.Tables("Section")
.DataBindings.Add("SelectedValue", bs, "Section")
End With
End Sub
But I want add item, like "---All---", so this is should be the output.
---All---
HR
Store
Packing
Training
Qc
Qa
Stock
Here's the simple solution
Dim dr As DataRow = dt.NewRow()
dr("Section") = "---All---"
dr("SectionId") = 0
dt.Rows.InsertAt(dr, 0)
With cbSection
.DisplayMember = "Section"
.ValueMember = "SectionId"
.DataSource = ds.Tables("Section")
.DataBindings.Add("SelectedValue", bs, "Section")
End With
cbSection.SelectedIndex = 0