How to add Items in the DatagridViewComboboxColumn of DatagridView during runtime - vb.net

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)

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"

Changing an existing datagridview column

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

How should I load all the data from a column to a datagridview combobox?

How can I make that the datagridview combobox loads with the items listed in the 'userid' column in my database? I want all the items to be listed on the datagridview combobox from the 'userid' column.
Here are the lines I've written so far:
Private Sub check()
cmd = New SqlCommand("Select UserID from info", con)
da = New SqlDataAdapter(cmd)
dt = New DataTable()
da.Fill(dt)
If dt.Rows.Count > 0 Then
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "UserID"
Dim dgvcolumn As New DataGridViewComboBoxColumn
With dgvcolumn
.Width = 150
.Items.Add(ComboBox1.Items)
.DataPropertyName = "UserID"
.HeaderText = "UserID"
End With
Else
End If
End Sub
Why not add an unbound column, type should be ComboBoxColumn. Then add a data source (named infobindingsource) that grabs the userid column from your info table and set the new column's data source to be the infobindingsource.
ilann

DataGridView ComboBoxCell is not showing values that are not from the list vb.net

Hi all i have a combobox which is nicely populated from a binding source. So the problem is the column of the datagrid "Name" is only showing values which comes from the populated drop down list. Those values that is not the same as the droplist is shown empty. can someone tell me why? below is they code for the combobox. I cant printscreen but the describtion is like this. The LIst has 3 names : John , Jake, Jay but the colum has over 10 name , each in its respective cell. The problem when onload it is not showing the other names.
Dim c4 As New DataGridViewComboBoxColumn()
c4.HeaderText = "Name"
c4.Name = "Name"
c4.DataPropertyName = "Name"
c4.DisplayMember = "NamesWithJ"
c4.ValueMember = "NamesWithJ"
c4.DisplayStyleForCurrentCellOnly = False
c4.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
c4.FlatStyle = FlatStyle.Standard
c4.SortMode = DataGridViewColumnSortMode.Automatic
c4.DataSource = AddtBndSrc
c4.Width = 100
Me.DataGrid.Columns.Add(c4)
And here is the code where i create the bindingsource and fill it with value. There is no error with the connection its showing perfectly. It just that the comboboxcolumn is showing null in some of the rows
Try
con = New SqlConnection(strConnection)
cib.Open()
adoAAda = New SqlDataAdapter(StrAddNameQuery, con)
adoAddtRs = New DataSet
adoAAda.Fill(adoAddtRs)
Dim tableAddt As DataTable = adoAddtRs.Tables(0)
Dim colum As DataColumn = tableAddt.Columns(0)
tableAddt.PrimaryKey = New DataColumn() {tableAddt.Columns(0)}
AddtBndSrc.DataSource = adoAddtRs.Tables(0)
con.close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Try
' connection procedure
con = New SqlConnection(strConnection)
con.Open()
adoPAda = New SqlDataAdapter(StrProductQuery, con)
adoProductsRS = New DataSet
adoPAda.Fill(adoProductsRS)
Dim tableProduct As DataTable = adoProductsRS.Tables(0)
Dim colum As DataColumn = tableProduct.Columns(0)
tableProduct.PrimaryKey = New DataColumn() {tableProduct.Columns(0)}
productBndSrc.DataSource = adoProductsRS.Tables(0)
MsgBox(tableProduct.Columns(0).ColumnName.ToString)
con.close
datagridview.Datasource = productBndSrc
Catch ex As Exception
MsgBox(ex.Message)
End Try
You want to merge values for combobox. Some are hardcoded into combo but are pulled from database. If so, you need to added the hardcoded values into datasource before binding to combo.
after following lines ADD THE CODE GIVEN BELOW.
adoAAda.Fill(adoAddtRs)
Dim tableAddt As DataTable = adoAddtRs.Tables(0)
'ADD YOUR HARD CODED NAMES TO DATASOURCE BEFORE BINDING
Dim tableAddt As DataTable = adoAddtRs.Tables(0)
Dim dr As DataRow = tableAddt.NewRow
dr("NamesWithJ") = "My test 1"
tableAddt.Rows.Add(dr)
Dim dr1 As DataRow = tableAddt.NewRow
dr("NamesWithJ") = "My test 2"
tableAddt.Rows.Add(dr1)
It seems you are assigning the combobox bindingSource AddtBndSrc to the DataGridViewComboBoxColumn datasource. So if that colum had some items assigned they will be overwritten by the datasource.

Programmatically populate DataGridView

I have a DataGridView that I'm trying to populate using a For loop:
Dim serverName As String = SQLServerName + "\" + Instance
Dim server As Server = New Server(serverName)
Dim Datatable1 As New DataTable
For Each database As Database In server.Databases
Dim row As DataRow = Datatable1.NewRow
row("Database") = database.Name
row("Version") = DBVersionCheck(serverName, database.Name)
row("Status") = My.Resources.My_Image
Datatable1.Rows.Add(row)
Next
DataGridView1.DataSource = Datatable1
The DGV has been designed with the designer (columns, layout etc).
Using the above the DGV does not populate. I was using a ListView for this but I need images in a subitem so have switched to using a DGV. Any advice?
You need to add the columns to the DataTable.
I've got some code (which is C#) but you should be able to convert it:
var columnSpec = new DataColumn
{
DataType = string,
ColumnName = "Database Name"
};
this.dataTable.Columns.Add(columnSpec);
which will add a column of type string with the name "Database Name".