Changing an existing datagridview column - sql

I want to change a datagridview column 'TeamAssignment' which loads from the database to have dropdowns (which is also populated from the database)
Dim conn As New SqlConnection(My.Resources.FCLRptConn)
Dim cmd As New SqlCommand("spFCLLUVTeamAssignment", conn)
Dim da As New SqlDataAdapter(cmd)
Dim TeamAssign As New DataSet
da.Fill(TeamAssign)
teamComBo.HeaderText = "Team Assignment"
teamComBo.DataPropertyName = "TeamAssignment"
teamComBo.DataSource = TeamAssign.Tables(0)
teamComBo.DisplayMember = "FCLTeamName"
teamComBo.ValueMember = "FCLTeamID"
dgvAgentAssignment.Columns.RemoveAt(5)
dgvAgentAssignment.Columns.Insert(5, teamComBo)
This is what I have used but I get an error that says that a column exists. How would I keep my existing column without having to remove it but populate it with the dropdowns?
Thanks

Related

Populate combobox in datagridview in VbNet From other table

I have a DataGridView with 3 TextBox columns and 1 ComboBox column. I have tried without result to populate the ComboBox column with data from other table, a suggestion will be apreciated.
This is the code
Dim conn As OleDb.OleDbConnection = DBConnect.getDbConnection()
dgv.Rows.Clear()
Try
Dim selectSql = "select idf,cb,rgso from libroacquisti "
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(selectSql, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Libroacquisti")
Dim dt As DataTable = ds.Tables("libroacquisti")
Dim row As DataRow
If dt.Rows.Count > 0 Then
For Each row In dt.Rows
Me.dgv.Rows.Add(row("idf"),row("cb"), row("rgso"),THIS IS COMBOBOX COLUMN AND I SHOULD WANT TO POPULATE IT FROM ANOTHER TABLE BY SQL QUERY )
Next row
End If
However, it must be considered that in each row of the DataGridView, in the related ComboBox, the data are always the same and are extracted from the same table.
In order to populate a combo box column from table data, you will need to set up a separate data set and binding source for the combo box to pull data from.
populate your dataset with your 'sub' table that will be referenced by the data from your data grid view. For a combo box, you will only need two columns. One for the id that will be referenced by the data in your DataGridView's ComboBoxColumn and one for the value that will be displayed.
Drop a new BindingSource control into your form designer
Set the DataSource property to the DataSet and set the DataMember property to the Table Name within the DataSet
Then set the ComboBoxColumn's DataSource property to your BindingSource and set its DisplayMember and ValueMember properties.
Now your DataGridView should have the appropriate value selected for each row and the combo box list populated showing the values populated in the data set that you set as the DisplayMember:
Dim selectSql = "select ValueColumn,DisplayColumn "
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(selectSql, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "ComboValues")
BSComboValues.DataSource = ds
BSComboValues.DataMember = "ComboValues"
Dim ComboColumn As DataGridViewComboBoxColumn = dgv.Columns("ComboColumnName")
ComboColumn.DataSource = BSComboValues
ComboColumn.ValueMember = "ValueColumn"
ComboColumn.DisplayMember = "DisplayColumn"

invalid column name RDLC Report Viewer says while populating data to data source using multiple data sources

I am trying to print an RDLC report that gets data from multiple data sources. The first data source is filled with data while the other data source gives the following error
"Invalid Column Name 'INV123'"
The code for filling the data sources is as follows:
frmInvoiceReport.DataTable_InvoiceTableAdapter.Fill(frmInvoiceReport.SIS_DBDataSet.DataTable_Invoice)
frmInvoiceReport.DataTable_InvoiceInfoTableAdapter1.Fill(frmInvoiceReport.SIS_DBDataSet.DataTable_InvoiceInfo)
'The DataSet created.
Dim myConnection As SqlConnection = New SqlConnection(cs)
Dim myDA As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [View_Invoice_Details] Where InvID = " & txtID.Text, myConnection)
Dim myDS As New DataSet
myDA.Fill(myDS, "DataTable_Invoice")
Dim myDa2 As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM [View_InvoiceInfo] Where InvoiceNo = " & txtInvoiceNo.Text, myConnection)
Dim myDs2 As New DataSet
myDa2.Fill(myDs2, "DataTable_InvoiceInfo")
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Clear()
Dim DtSrc As ReportDataSource = New ReportDataSource("SIS_DBDataSet", myDS.Tables("DataTable_Invoice"))
Dim DtSrc2 As ReportDataSource = New ReportDataSource("SIS_DBDataSet", myDs2.Tables("DataTable_InvoiceInfo"))
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Add(DtSrc)
frmInvoiceReport.ReportViewer1.LocalReport.DataSources.Add(DtSrc2)
frmInvoiceReport.ReportViewer1.RefreshReport()
frmInvoiceReport.ShowDialog()
I am not sure where the error arises. I have debugged the code and it catches an exception saying that the column name is invalid. I need a quick fix for that.

Add only new items from listbox to SQL

i have a listbox that users can add Countries Names into it anytime.
now i need to only add new data to check if the data is new then insert it to sql.
my code not work:
For Each i As String In listbox1.Items
Dim sql = "select * From Countries where CountryName=N'" & i & "'"
Dim adp As New SqlClient.SqlDataAdapter(sql, SQlconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
Dim dr = dt.NewRow
dr!CountryName = i
dt.Rows.Add(dr)
Dim cmd As New SqlClient.SqlCommandBuilder(adp)
adp.Update(dt)
End If
Next
If you're loading the existing names from the database in the first place then there's no need for you to check anything. Simply query the database to populate a DataTable and bind that to the ListBox. When the user adds a new country, add it to the DataTable and it will automatically show up in the ListBox. When it's time to save, just use the same data adapter to save the DataTable and only the new records will be saved.

Dynamically add listbox columns based on columns in an Access Database?

I would like to do a populate a column in my listbox for each field in my Access Database.
Right now I have to manually add the field:
QUERBOX.Columns.Add("Requestor Name", 200, HorizontalAlignment.Left)
How can I adapt my code below to automatically add columns each time I run the sub?
Dim queryString As String = "SELECT * FROM Table1;"
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_reader As OleDbDataReader
querbox.clear
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\apt.accdb")
connection.Open()
command = New OleDbCommand(queryString, connection)
data_reader = Command.ExecuteReader
If data_reader.HasRows Then
While data_reader.Read
Dim newitem As New ListViewItem()
newitem.Text = data_reader.GetValue(0) 'first column
newitem.SubItems.Add(data_reader.GetValue(1)) 'second column
QUERBOX.Items.Add(newitem)
End While
End If
As Plutonix suggested in comments, a DataGridView would probably be best suited, but for the sake of answering the question here's how I've done something similar:
connection.Open()
'' Fill a DataTable with all of the Column schema for the given table of 'Table1'
Dim schemaTable As DataTable = connection.GetSchema("Columns", New String() {Nothing, Nothing, "Table1", Nothing})
'' Iterate through each column in the Schema Table
For i = 0 To schemaTable.Rows.Count - 1 Step 1
'' Declare a new item for the list
Dim newItem As New ListViewItem()
newItem.Text = schemaTable.Rows(i)!COLUMN_NAME.ToString()
newItem.SubItems.Add(schemaTable.Rows(i)!COLUMN_NAME.ToString()
'' Add new item to the interface
QUERBOX.Items.Add(newItem)
Next
connection.Close()
This has helped me in projects where the user is familiar with the database structure and may need to select the fieldname as part of the business logic. For example, allowing someone with little database knowledge to standardize the records within a given field.

How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime

I have a datagridview with three columns which is filled using Datareader. There is a DatagridViewComboboxcolumn in the datagridView.
I want this DatagridViewComboboxcolumn should also be filled using datareader.
Please suggest how can i add items to DatagridViewComboboxcolumn using Datareader.
Below is the code that i have tried.
Here dr is SqlDatareader
Datagridview.Rows.Add(dr("Column1").ToString, dr("Column2"),dr("DatagridViewComboboxcolumn "))
But when i add this way im getting error on the DatagridViewComboboxcolumn Column.
Please Suggest
As previously mentioned, you cannot set the DataSource of a DataGridViewColumn to a DataReader (since this is a forward only, database connected object). What you can however do is fill a DataTable and the set the DataSource on the DataGridViewColumn to this DataTable. You will also need to set the DataPropertyName (this is the column name of the DataGridView's datasource), ValueMemeber and DisplayMember. The example below uses the Adventureworks DB and populates a DataGridView with 4 columns (one of which is a combobox -- ProductIdCombo). Simply create a form, drop a DataGridGridView control on it named DataGridView1 and run the following. The ProductId column demonstrates that the underlying column bound to the combo (ProductIdCombo column)is indeed updating the ProductId field in the dtProductsInventory DataTable.
Dim dtProductInventory As New System.Data.DataTable
Dim dtProducts As New System.Data.DataTable
Using objSqlServer As New System.Data.SqlClient.SqlConnection("Server=LOCALHOST\SQLEXPRESS; Integrated Security=SSPI;Initial Catalog=AdventureWorks")
objSqlServer.Open()
Dim sqlCmd As New System.Data.SqlClient.SqlCommand("select * from production.ProductInventory", objSqlServer)
dtProductInventory.Load(sqlCmd.ExecuteReader)
sqlCmd.CommandText = "Select * from production.product"
dtProducts.Load(sqlCmd.ExecuteReader)
End Using
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = dtProductInventory
Dim colProductIdCombo As New System.Windows.Forms.DataGridViewComboBoxColumn()
colProductIdCombo.DataSource = dtProducts
colProductIdCombo.DisplayMember = "Name"
colProductIdCombo.ValueMember = "ProductId"
colProductIdCombo.DataPropertyName = "ProductId"
colProductIdCombo.HeaderText = "ProductIdCombo"
DataGridView1.Columns.Add(colProductIdCombo)
Dim colProductId As New System.Windows.Forms.DataGridViewTextBoxColumn()
colProductId.DataPropertyName = "ProductId"
colProductId.HeaderText = "ProductId"
DataGridView1.Columns.Add(colProductId)
Dim colShelf As New System.Windows.Forms.DataGridViewTextBoxColumn()
colShelf.DataPropertyName = "Shelf"
colShelf.HeaderText = "Shelf"
DataGridView1.Columns.Add(colShelf)
Dim colQuantity As New System.Windows.Forms.DataGridViewTextBoxColumn()
colQuantity.DataPropertyName = "Quantity"
colQuantity.HeaderText = "Quantity"
DataGridView1.Columns.Add(colQuantity)