How to populate the Combobox in VBdotNet - vb.net

How to pupolate the Combobox in VB.Net, the items are from DataSet... Here's my code
Dim LibDSPopulate As New DataSet
Dim LibDAPopulate As OdbcDataAdapter = New OdbcDataAdapter("SELECT DISTINCT Category FROM tblBooks", LibConn)
LibDAPopulate.Fill(LibDSPopulate, "tblBooks")
cmbCategoryF.Items.Add(LibDSPopulate)

You may use Data binding facility.
'Add an empty entry
Dim dr As DataRow = LibDSPopulate.Tables("tblBooks").NewRow
dr("Category") = "***Select***"
LibDSPopulate.Tables("tblBooks").Rows.InsertAt(dr, 0)
cmbCategoryF.DataSource=LibDSPopulate.Tables("tblBooks")
cmbCategoryF.DisplayMember="Category" 'Name of field
cmbCategoryF.ValueMember="Category" 'Name of field
If you don't want to use databindng then add each item using Items.Add() method.
 For Each row As DataRow In LibDSPopulate.Tables("tblBooks").Rows
ComboBox1.Items.Add(row("Category"))
Next

Related

Parsing through DataTable for matching value and creating a new DataTable from the results

In VB.NET I'm trying to count through the value's in a list, until the value for "stock" in my DataTable, Boxes, is equal to the value in my list. When this occurs a row should be created in my new DataTable "output". I would then continue counting through the list looking for other matching instances to add into "output".
So far counting through the list and then within that count going through the datatable to match value's works wonderfully. The part where I'm getting hung up is when I try to take the matching row and put it into another table.
Dim output As DataTable
Dim jsonstringy As String = BoxComms.WebGet("http://foo.foo.foo") 'PULLS JSON STRING
Dim Boxes = Newtonsoft.Json.JsonConvert.DeserializeObject(Of DataTable)(jsonstring)
Dim MyString As String = TextBox1.Text 'MAKE STRING OF STOCK #'s FROM TEXTBOX
MyString = MyString.Replace(" ", "") 'GET RID OF SPACES
Dim MyArray() As String = MyString.Split(",") 'SEPERATE COMMA DELIMETED LIST
Dim MyList As List(Of String) = MyArray.ToList()
For Each value In MyList 'COUNT THROUGH LIST OF STOCK #'s
For Each row As DataRow In Boxes.Rows
If row("stock") = value Then 'IF STOCK # IS EQUAL TO ANY OF NUMBERS IN TEXTBOX
output.ImportRow(row) 'ADD ROW FROM DATATABLE Boxes to DATATABLE output
End If
Next row
Next
The solution was fairly simple, my DataTable needed a schema first. in order to copy the schema all I needed was to initialize the new table as a clone. to do so the code was as follows.
Dim output As DataTable = Boxes.Clone()
clone vs copy. copy get's the schema and data clone just gets the schema. then as I was parsing through the first table looking for matches I had to add the row to my new table with the following code.
output.Rows.Add(row.ItemArray)
the full code is as follows
Dim jsonstring As String = BoxComms.WebGet("https://foo.foo.foo")
Dim Boxes = Newtonsoft.Json.JsonConvert.DeserializeObject(Of DataTable)(jsonstring)
Dim output As DataTable = Boxes.Clone()
Dim MyString As String = TextBox1.Text
MyString = MyString.Replace(" ", "")
Dim MyArray() As String = MyString.Split(",")
Dim MyList As List(Of String) = MyArray.ToList()
For Each value In MyList
For Each row As DataRow In Boxes.Rows
If row("stock") = value Then
Console.WriteLine(row("stock"))
output.Rows.Add(row.ItemArray)
End If
Next row
Next
BoxControl.DataGridView1.DataSource = output
Me.Close()
End If

how to increase datatable's name number in vb.net

I know, how to increase integer, string by for statement
but I want know how to change the name by 'for ~next' statement.
For example,
Dim row_sort1 As DataTable = rows1.CopyToDataTable
Dim row_sort2 As DataTable = rows2.CopyToDataTable
Dim row_sort3 As DataTable = rows3.CopyToDataTable
Dim row_sort4 As DataTable = rows4.CopyToDataTable
Dim row_sort5 As DataTable = rows5.CopyToDataTable
Dim row_sort6 As DataTable = rows6.CopyToDataTable
Dim row_sort7 As DataTable = rows7.CopyToDataTable
I had coding like this,, bad cording So I want change by 'for ~next' statement.
I want increase the datatable name's number (1~7)
how can reflect in this coding. I want fix my coding more simple and useful.
I need your help
thank you
I assume you have a Collection of Rows which is your master where you want to copy from.
I have a similiar approach like OSKM. Better use a list collection than an array.
To access tables afterwards in the collection you can use Linq.
' Given master rowcollection
Dim masterRow As EnumerableRowCollection(Of DataRow)
' Empty table collection
Dim tableList As New List(Of DataTable)
For t As Integer = 0 To 6
' copy Master to a new table
Dim newTable As DataTable = masterRow.CopyToDataTable()
' give the new table a name
newTable.TableName = "Table" & t.ToString
' Add new table to collection
tableList.Add(newTable)
Next
' Access a certain table (i.e. Table5) using Linq
Dim table5 As DataTable = tableList.FirstOrDefault(Function(x) x.TableName = "Table5")
If i understand your question correct the following might help.
'Note your datatables will be named Datatable0 to Datatable6
Dim DTs(6) As DataTable
For i = 0 To 6
DTs(i) = New DataTable
DTs(i).TableName = "Datatable" & i
Next
There is probably better ways but this will work!

Change data according to selected item in combobox

I call into a function which runs a query into SQLServer and returns a DataTable with multiple columns. I want the data from the DataTable to change rows according to the selected item in the combobox.
Dim dt As New DataTable
Dim rInt As New Int32
dt = subGetDB()
rInt = ComboBox1.SelectedIndex
Textbox1.Text = dt.Rows(rInt)("description").ToString()
TextBox2.Text = dt.Rows(rInt)("accountFilter").ToString()
Am I on the right track with this so far?
Yes ... but you need to put all of the code above into the SelectedValueChanged event for the ComboBox1 object.

How to import csv file in Datagridview with certain columns and rows?

I'm new to VB.net and I don't know how to display certain columns and rows in datagridview that was imported from CSV file. My problem is I have many columns and all I want to display is 2 columns:
Name,Age,Mobile Number,ID number
Alex,18,09848484841,0010
George,19,02987654321,0020
Toni,17,09277470257,0030
How can I display only the Name & Age columns and its rows?
If you use a datatable you get the data structure and collection together. something like this:
Dim sr As New IO.StreamReader(filename)
Dim dt As New DataTable
Dim newline() As String = sr.ReadLine.Split(","c)
dt.Columns.AddRange({New DataColumn(newline(0)), _
New DataColumn(newline(1))})
While (Not sr.EndOfStream)
newline = sr.ReadLine.Split(","c)
Dim newrow As DataRow = dt.NewRow
newrow.ItemArray = {newline(0), newline(1)}
dt.Rows.Add(newrow)
End While
DataGridView1.DataSource = dt
Use a custom class with properties that match the data you want to store and make an instance of that class for each row of data use read, then have a List(Of {custom class}) to hold each object and the DGV's DataSource property can view the collection in the grid. The property names in the class will be used as the header.

vb.net working with multiple controls added at runtime

I am adding a tab page and datagridview to a Tab Control for every record in a datatable.
I would like to have a new Tab/DataGridView for each record (there will be ~3 for right now). I am declaring a new DataGridView D. How do I refer to these controls later?
I will want to do things like save changes in the datagridview to the database. Currently I can get the data on the screen and it looks good, but I believe I am not adding the DataGridView controls correctly because I keep re-using "D" as the control.
Dim dt As New DataTable
GetDataTable("SELECT * FROM aFeeTypes DescSeq", dt)
Dim i As Integer
'for each class in the datatable add a tab and a datagridview
For i = 0 To dt.Rows.Count - 1
Dim dr As DataRow
dr = dt.Rows(i)
Dim D = New DataGridView
D.Visible = True
Dim tp As New TabPage
tp.Name = "tp" & i
tp.Text = dr.Item("Desc2")
frmUI.tcFee.TabPages.Add(tp)
frmUI.tcFee.TabPages(i).Controls.Add(D)
dgv_Fill(D, "SELECT * FROM Fee WHERE ClassID=" & dr.Item("ClassID") & " ORDER BY Seq")
D.AutoResizeColumns()
D.Width = tp.Width
D.Height = tp.Height
Next i
this does not work:
With frmUI.Controls("D" & i)
.AutoResizeColumns()
.Width = tp.Width
.Height = tp.Height
End With
D is purely the variable name in the scope you are using it in.
You need to give the control a unique Name that yo can reference it by later.
The Name property can be used at run time to evaluate the object by
name rather than type and programmatic name. Because the Name property
returns a String type, it can be evaluated in case-style logic
statements (Select statement in Visual Basic, switch statement in
Visual C# and Visual C++).
I found a solution to the problem. The problem is that the new row has an autonumber ID.
When da.update(dt) occurs, the new row is inserted to the database. The database knows the new autonumber ID. However, the datatable does not.
For the datatable to know, the dataadapter is refilled. However, this causes the datatable to have all the old rows plus all the new rows and they all appear in the datagridview.
By clearing the old rows from the datatable, the fill refreshes all the data in the datatable and therefore refreshes the datagridview.
Public Sub dgv_AddRow(ByVal dgvName As String)
Dim dgv As DataGridView = colDataGridView.Item(dgvName)
Dim da As SqlDataAdapter = colDataAdapter.Item(dgvName)
Dim dr As DataRow = frmUI.allDataSet.Tables(dgvName).NewRow
'autopopulate the class id
dr("ClassID") = dgv.Parent.Tag
'add the new row
frmUI.allDataSet.Tables(dgvName).Rows.Add(dr)
'get the changed table
Dim dt As DataTable = frmUI.allDataSet.Tables(dgvName)
'inserts the new row to the database. concurrency violation
'will occur because datatable does not know the new Autonumber ID for the new row.
da.Update(dt)
'remove the datatable rows so they can be replaced using the adapter fill
'if rows are not cleared, following fill causes datatable to
'have existing rows plus all the rows again due to the fill
frmUI.allDataSet.Tables(dgvName).Rows.Clear()
'refill the adapter (refill the table)
'Everything you do to the underlying dataTable
'gets displayed instantly on the datagridview
da.Fill(frmUI.allDataSet, dgvName)
End Sub