How to enable on BindingNavigator the AddNewItem - vb.net

I'm trying to bind the BindingNavigator to a BindingSource. It works very good, but the problem is the yellow plus icon is disabled. How can I make it enabled?
I created manually with code the dataset, tableadapters and the bindingsource, but when I bind it on the bindingnavigator it shows the records but it doesn't enable "Delete" and "Add Item".
What im doing wrong?
Code from comment:
Dim connstr As String = "Data Source=" + Application.StartupPath + "\Prueba.sdf"
Dim conn As New SqlCeConnection(connstr)
Dim cmd As New SqlCeCommand("SELECT * FROM datos", conn)
Dim inscmd As New SqlCeCommand("INSERT INTO datos VALUES (#nombre,#apellido,#id)", conn)
dt = New SqlCeDataAdapter(cmd)
dt.Fill(DataSet1, "datos")
dt.InsertCommand = inscmd
BindingSource1.DataSource = DataSet1
DataGridView1.DataSource = BindingSource1
DataGridView1.DataMember = "datos"
DataGridView1.Columns("id").Visible = False

Since you provided no code, you made it really hard to try to solve your problem.
Here is an example that works. Take a new form, drop a TextBox and BindingNavigator controls on it.
Add this code:
Public Class Form1
Private bs As New BindingSource
Private ds As New DataSet
Private dt As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt.TableName = "English"
dt.Columns.Add("Words")
dt.Rows.Add("Hello")
dt.Rows.Add("Good-Bye")
ds.Tables.Add(dt)
bs.DataSource = ds
bs.DataMember = "English"
TextBox1.DataBindings.Add("Text", bs, "Words")
BindingNavigator1.BindingSource = bs
End Sub
End Class
The TextBox should have "Hello" in it. The BindingNavigator should have "1 of 2" on it, plus the Add and Delete buttons should be enabled and working.
If this doesn't help solve the problem, you will have to post some code to help us reproduce the problem.

For fun I added a 2nd text box on the form, a 2nd column in the table, and populated the two fields for both records.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dt.TableName = "English"
dt.Columns.Add("Words")
dt.Columns.Add("Spanish")
dt.Rows.Add("Hello", "Hola")
dt.Rows.Add("Good-Bye", "Adios")
ds.Tables.Add(dt)
bs.DataSource = ds
bs.DataMember = "English"
TextBox1.DataBindings.Add("Text", bs, "Words")
TextBox2.DataBindings.Add("Text", bs, "Spanish")
BindingNavigator1.BindingSource = bs
End Sub

Related

Insert, Delete, and Edit Data in DataGridView using single Update Button in Vb.net

I am trying to have a add, delete, and edit function in Vb.net by using one update button and changing the values in the datagridview manually then hitting update. However, I get an error stating
"System.InvalidOperationException: 'Update requires a valid UpdateCommand when passed DataRow collection with modified"
Any Ideas? The error comes next to the da.Update(changes)
Also in the above code not shown in my code I have load_data() in the private form1_load sub.
Here is code:
Dim da As New SqlDataAdapter
Dim dt As New DataSet
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim cmd As New SqlCommandBuilder
Dim changes As New DataSet
changes = dt.GetChanges
If changes IsNot Nothing Then
da.Update(changes)
da.Fill(dt)
DataGridView1.DataSource = dt.Tables(0)
End If
End Sub
Private Sub load_data()
Using connection = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=vbconnectionfinal;Integrated Security=True")
da = New SqlDataAdapter("Select * From TrueTrack", connection)
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt.Tables(0)
End Using
End Sub
You are creating a SqlCommandBuilder but not associating it with a SqlDataAdapter, so what's the point? The whole purpose of a command builder is to automatically generate action commands when you call Update on a data adapter. When you create your data adapter, create the command builder immediately after and associate the two, which you would do by passing the data adapter as an argument to the constructor of the command builder.
The following code works for me.
Dim da As New SqlDataAdapter
Dim dt As New DataSet
Dim connection = New SqlConnection("connection string")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cmd As New SqlCommandBuilder(da)
Dim changes As New DataSet
changes = dt.GetChanges
If changes IsNot Nothing Then
da.Update(changes)
DataGridView1.DataSource = dt.Tables(0)
End If
End Sub
Private Sub load_data()
Dim cmd As SqlCommand = New SqlCommand("Select * From TrueTrack", connection)
da = New SqlDataAdapter(cmd)
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt.Tables(0)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
load_data()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
connection.Close()
End Sub

Datagridview - refresh after DB Update on another form

I'm setting Datagridview DataSource in Form1. In Form 2 I update those records (works fine in DB), and after updating them I want to refresh Datagridview in Form1 too.
Here is my code in Form1_Button_Click:
OracleConn() 'connection to my DB
Using cmd As New OracleCommand()
Dim SQL As String = "Select * FROM MyTable"
If Chk1.Checked = True Then
cmd.Connection = OracleConn()
cmd.CommandText = SQL
cmd.CommandType = CommandType.Text
Dim dt1 As New DataTable
Using dad As New OracleDataAdapter(SQL, OracleConn)
dad.Fill(dt1)
End Using
End if
DataGridView1.DataSource = dt1
End Using
How can I do this most easily ?
Write down a method to fill your data grid in Form1
public sub FillData()
OracleConn() 'connection to my DB
Using cmd As New OracleCommand()
Dim SQL As String = "Select * FROM MyTable"
If Chk1.Checked = True Then
cmd.Connection = OracleConn()
cmd.CommandText = SQL
cmd.CommandType = CommandType.Text
Dim dt1 As New DataTable
Using dad As New OracleDataAdapter(SQL, OracleConn)
dad.Fill(dt1)
End Using
End if
DataGridView1.DataSource = dt1
End Using
end sub
Call the method into your button click ( Form1_Button_Click)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FillData()
End sub
Now you can call the method of Form1 from Form2 directly after update,
Form1.FillData()
Records will reflect in Form 1
NB: Method should be public in access.
Instead of set
DataGridView1.DataSource = dt1;
Use a global BindingSource object, and bind it to both DataGridView, then all the changes made in one DataGrid will be reflected in the other.
BindingSource bsData = new BindingSource();
bsData.DataSource = dt1;
DataGridView1.DataSource = bsData;
DataGridView2.DataSource = bsData;
In your 2nd Form, bind the controls, TextBox, ComboBox, etc to this BindingSource and again, you don't need to refresh de DataGridView.

Pass DataTable to ReportViewer

I am trying to pass a datatable to a reportviewer which I fill by code, is there a way to do that? I tried this but nothing happened:
Dim bs As BindingSource
bs = New BindingSource()
bs.DataSource = DataTablefillbycode
Dim rs As ReportDataSource
rs = New ReportDataSource()
rs.Name = "Tabletest"
rs.Value = bs
form2.ReportViewer1.RefreshReport()
form2.ReportViewer1.Reset()
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rs)
form2.ReportViewer1.RefreshReport()
form2.ShowDialog()
PS : The GridView works fine with the table "Tablefillbycode"
Follow these steps to be able to pass data table to your report:
I suppose you created a Report1.rdlc in root of your project Test, so the name of its embedded resource would be Test.Report1.rdlc. Also I suppose the name of DataSet in your Report1 is DataSet1.
Put a report viewer on your Form2 and set its Dock property to Fill and set its Modifier property to Public.
In Form1 I suppose you have a DataGridView1 that you want to fill it in the Form_Load and you will use the same query that you used for creating report.
In Form1 I suppose you have a Button1 that you want to show Form2 when you click on Button1 and you want pass the data of DataGridView1 to it.
Don't forget to Imports Microsoft.Reporting.WinForms in Form1
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cn = "data source=(localdb)\v11.0;initial catalog=TestDB;integrated security=True;"
Dim cmd = "SELECT Id,Name FROM Category"
Dim adapter = New SqlDataAdapter(cmd, cn)
Dim table = New DataTable()
adapter.Fill(table)
Me.DataGridView1.DataSource = table
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 = New Form2()
Dim rds= New ReportDataSource("DataSet1", Me.DataGridView1.DataSource)
form2.ReportViewer1.LocalReport.DataSources.Clear()
form2.ReportViewer1.LocalReport.DataSources.Add(rds)
form2.ReportViewer1.LocalReport.ReportEmbeddedResource = "Test.Report1.rdlc"
form2.ShowDialog()
End Sub
Screenshot:

Filtering Data within a DataGridView in VB.NET

How would I add a filtration function to my program so when I begin typing into a text box, it filters the results in the DataGridView? Just to clarify, this code loads a linked Access database into the DataGridView on form load and allows me to add, remove and update the data contained with in.
I'm relatively new to Visual Basic and I have ran out of ideas in regards to what to in order to get it to work. Thank you in advance for any help provided!
Imports System.Data.OleDb
Public Class frmDatabase
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim dt As New DataTable
Dim da As New OleDbDataAdapter
Private Sub frmDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Joe\Documents\Visual Studio 2012\Projects\school database viewer\school database viewer\dbSchoolDatabase.mdb"
con.Open()
ds.Tables.Add(dt)
da = New OleDbDataAdapter("Select * from tableStudentDetails", con)
Dim cb = New OleDbCommandBuilder(da)
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
da.Fill(dt)
dgvStudentDetails.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub cmdSave_Click_1(sender As Object, e As EventArgs) Handles cmdSave.Click
da.Update(dt)
End Sub

Issue with Comobox on DataGridView

I am using VB.NET to pull some data from an SQL database into a Datagridview.
I then want the user to be able to modify the information and save it back to the database, which I have working fine.
What I need to be able to do now is to restrict the values, but way of a combobox for the field Tarrif.
I have configured a DataSource called Tarrif1 and I am using the below code.
I have a couple of issues/questions.
Firstly the dropdown shows a single value of "System.Data.DataViewManagerListItemTypeDescriptor" not the Tarrif1 values.
Secondly, I now have 2 columns on my datatable called Tarrif, the original database one and the one I have added - How can I get the ComboBox to right back to the appropriate Tarrif database field.
Here is my code:
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT * FROM Customers"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
'comboBoxCol.DisplayMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'comboBoxCol.ValueMember = "Tarrif" // when I add these rows the new Tarrif column is not visible
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class
Hope that makes sense. Any help greatly appreciated.
A few things in this code:
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
comboBoxCol.DataSource = Tarrifs1
First, I'm not sure what happens whenever the code attempts to fill a DataSet that already has data in it. In my code, I never assume, so I always reinitialize it each time unless I need it:
ds = new DataSet()
Next, you are setting the DataSource for comboBoxCol as Tarrifs1, but I do not see that defined anywhere. You might want to look into that.
Finally, about the SQL: Dim sql As String = "SELECT * FROM Customers" I don't know what columns are in your Customers table. Is Tarrif an actual column?
I figured half my issue out.
I configured a second data connection to pull the Tarrif drop down from the Tarrifs table and display this as part of the customer data table.
My outstanding issues now, is that I need to write the select Tarrif value back to the Customers table.
All the other updated values save, just not sure how to write the Tarrif dropdown back to my SQL table
Here is my latest code.
Imports System.Data.SqlClient
Imports System.Data.Common
Public Class ViewCustomersForm
Dim ds As DataSet = New DataSet()
Dim connStr As String = "server=barry-laptop\SQLEXPRESS; database=BillingReferenceData; integrated security=yes"
Dim sql As String = "SELECT [Customer ID] ,[Customer Name] ,[Address] ,[City] ,[County] ,[Post Code] FROM [BillingReferenceData].[dbo].[Customers]"
Dim conn As SqlConnection = New SqlConnection(connStr)
Dim comm As SqlCommand = New SqlCommand(Sql, conn)
Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
Dim con As New System.Data.SqlClient.SqlConnection("server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes")
Dim strSQL As String = "SELECT * FROM Tarrifs"
Dim da As New System.Data.SqlClient.SqlDataAdapter(strSQL, con)
Private Sub ViewCustomersForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'---open the connection and fill the dataset---
conn.Open()
dataadapter.Fill(ds, "Customers_table")
conn.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers_table"
'---create a combobox column---
Dim comboBoxCol As New DataGridViewComboBoxColumn
'---set the header---
comboBoxCol.HeaderText = "Tarrifs"
'---data bind it---
da.Fill(ds, "Tarrifs")
comboBoxCol.DataSource = ds.Tables("Tarrifs")
comboBoxCol.DisplayMember = "Tarrif"
comboBoxCol.ValueMember = "Tarrif"
'---add a combobox column to the DataGridView control---
DataGridView1.Columns.Add(comboBoxCol)
End Sub
Private Sub Button_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCmdBuilder As New SqlCommandBuilder(dataadapter)
sqlCmdBuilder.GetUpdateCommand()
dataadapter.Update(ds.Tables("Customers_table"))
dataadapter.Update(ds.Tables("Tarrifs"))
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
End Class