Programmatically populate DataGridView - vb.net

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".

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"

Search data from SQL Server and Display onto textbox and data grid VB.Net

I want to retrieve data from SQL Server and display into Textbox and DataGrid. I want the data from request box to be display into the textbox and the leftjoin table to be diplayed in datagrid.
con.Open()
cmd.Connection = con
cmd.CommandText = "select * from requestbox left join requisitiondata on requisitiondata.requestdata_id = requisitiondata.requestdata_id where request_box = '" & txtsearch.Text & "'"
cmd.ExecuteNonQuery()
Dim TABLE As New DataTable
With da
.SelectCommand = cmd
.Fill(TABLE)
End With
cbspayment.Text = ("spayment").ToString()
cbsoption.Text = ("soption").ToString()
txtto.Text = ("to1").ToString()
txtsupplier.Text = ("supplier").ToString()
txtterms.Text = ("terms").ToString()
txtreference.Text = ("reference").ToString()
txtfrom.Text = ("from1").ToString()
txtcharge.Text = ("charge").ToString()
If you only want some columns of your data to be displayed in your grid then you should add the desired columns to the grid in the designer. You can specify what column in the data source a grid column should bind to by setting its DataPropertyName. You can then bind your data to the grid and other controls as well if desired. As an example:
Add a DataGridView, a BindingSource and a TextBox to your form.
Add a single text box column to the grid and set its DataPropertyName to "Name".
Create a handler for the Load event of the form and add the following code:
Dim table As New DataTable
With table.Columns
.Add("Name", GetType(String))
.Add("Description", GetType(String))
End With
With table.Rows
.Add("One", "First")
.Add("Two", "Second")
.Add("Three", "Third")
End With
BindingSource1.DataSource = table
DataGridView1.AutoGenerateColumns = False
DataGridView1.DataSource = BindingSource1
TextBox1.DataBindings.Add("Text", BindingSource1, "Description")
When you run the project, you will see the Name column's data displayed in the grid and, when you select a row, you will see the corresponding value from the Description column displayed in the TextBox. You can apply the same principle no matter how many columns you want displayed in the grid, no matter how many TextBoxes you have and no matter how you populate your DataTable.

Combo box isn't filling from DB table that is different than the datagridview data source

I have a datagridview in my application that has its DataSource set to pull from the MemberInfo table in my DB:
m_DataAdapt = New SqlDataAdapter("Select * FROM memberInfo ORDER BY lName ASC", m_DBConn)
m_CommBuild = New SqlCommandBuilder(m_DataAdapt)
m_DataSet = New DataSet()
m_DataAdapt.Fill(m_DataSet)
dgvMemberInfo.AutoGenerateColumns = False
dgvMemberInfo.DataSource = m_DataSet.Tables("Table")
This works as expected and gives me all the correct columns. However I want the Rank and Role columns to be combo boxes that get their options from a different table in the DB:
https://imgur.com/a/ZxOiBsw
However when I run the application and choose the drop downs, they are empty and there are no errors thrown. As far as I've seen so far this should work. Is there a setting/code line somewhere else that I have to do?
For whatever reason, setting it in the Designer did nothing, but setting the same stuff through code worked:
Dim b_rankAdapt As SqlDataAdapter
b_rankAdapt = New SqlDataAdapter("Select * From availRanks", m_DBConn)
Dim dt As DataTable
dt = New DataTable
b_rankAdapt.Fill(dt)
For Each iRow As DataGridViewRow In dgvMemberInfo.Rows
RankDataGridViewTextBoxColumn.DataSource = dt
RankDataGridViewTextBoxColumn.ValueMember = "rankID"
RankDataGridViewTextBoxColumn.DisplayMember = "rankName"
Next
I didn't try this before because I didn't know I could reference the cell directly through RankDataGridViewTextBoxColumn.

Adding a new table to Xml Dataset by the click of a button in VB .Net

I am trying to find out how and if it is possible to add a new table to a dataset by letting the end user select the name and create a new table and submit it. When the user clicks submit I am planning on it adding the name to a combo box on another form and when they select it, it will show the content of that table. If anyone could give me advice it would be greatly appreciated.
Dim dataset as New DataSet
Dim table as New DataTable("tablename")
'add structure of datatable
Dim col As DataColumn
col = New DataColumn
col.ColumnName = "col1"
col.DataType = GetType(String)
table.Columns.Add(col)
col = New DataColumn
col.ColumnName = "col2"
col.DataType = GetType(String)
table.Columns.Add(col)
'add to dataset
dataset.Tables.Add(table)

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)