Cannot get the text of multiple selected items in a VB.NET ListBox because it's of type "datarowview" - vb.net

I've got listbox1 in VB 2010, which is bound to a data source and displays values from a dataset. I bound it using the Designer - i.e. not via the code. I just selected the datasource in the properties of listbox1.
Now I want to retrieve the selected values. When I leave the listbox as single select, then ListBox1.SelectedValue.ToString does the job - it gives me the text of the selected item.
But I need it to allow multiple selections.
This is my code:
Dim items As ListBox.SelectedObjectCollection
items = ListBox1.SelectedItems
For Each i As String In items
MsgBox(i)
Next
And this is the error I get:
Conversion from type 'DataRowView' to type 'String' is not valid.
I've tried a few different ways to get the values of the selected items but there doesn't seem to be any straightforward way to do it. Is it impossible? Is it necessary to declare a new dataset and fill the listbox programmatically or something?

For Each drv As DataRowView In ListBox1.SelectedItems
MessageBox.Show(drv.Item(0).ToString)
Next

Off the top of my head I think you can do:
Dim items As ListBox.SelectedObjectCollection
items = ListBox1.SelectedItems
For Each i As ListViewItem In items
MsgBox(i.Value.ToString())
Next

Check out my question and answer. It allows you to break out the individual entries from a Database into list boxes.
Add 2 List Boxes and a text box to a form then copy this code, You will have to substitute your own Server and database etc entries.
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' get the data
Dim SQLConnectionString As String = "Data Source=HL605\RIVWARE;Database=RIVWARE;Integrated Security=true;"
Dim mySQLConnection As New SqlConnection(SQLConnectionString)
' Populate the list box using an array as DataSource.
mySQLConnection.Open()
Dim SQLDataTable As New System.Data.DataTable
'Create new DataAdapter
Dim mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM [Rivware].[dbo].[RivetTypes]", mySQLConnection)
mySQLDataAdapter.Fill(SQLDataTable)
ListBox1.DataSource = SQLDataTable
ListBox1.DisplayMember = "RivetType"
' remove validation data from list
Dim Count As Integer
Dim X As Integer
'get the column of data to search for
For Count = 0 To ListBox1.DataSource.Columns.Count - 1
X = InStr(ListBox1.DataSource.Columns.Item(Count).ToString, "Name")
If X <> 0 Then Exit For
Next
' now search for any invalid rows, in that column. those containing "---"
Dim TheTable As DataTable = CType(ListBox1.DataSource, DataTable)
Dim theRow As DataRow() = TheTable.Select()
Dim RowNumber As Integer
For RowNumber = 0 To UBound(theRow) - 1
If theRow(RowNumber).Item(Count).ToString <> "---" Then
' data is OK so transer it to the other listbox
ListBox2.Items.Add(theRow(RowNumber).Item(Count - 1).ToString)
End If
Next
ListBox2.ClearSelected()
End Sub 'NewNew
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
TextBox1.Text = ListBox2.SelectedItem.ToString()
End Sub
End Class 'ListBoxSample3

Related

Stuttering DataGridView in VB

I have a DataGridView that is bound to a datatable with hundred of rows, the database is a simple flatfile database written to a txt file. Whenever I scroll to the bottom the DGV starts stuttering. I am thinking of solutions but cannot find a way to code them
Here are my proposed solution:
Use Paging to lessen the numbers of row being rendered. Here's is a similar solution but they are using sql
Using doublebuffer which I've never touched before. I've tried doing DGV.doublebuffer = true but it said DGV is protected
Any help or clarification on my problem are greatly appreciated
Edit: Here is a GIF of how my DGV is stutteting
The Datatable is named Tbl_Sample
Here is how I insert rows of data into data table. It gets data using System.IO on the Flatfile database(.txt file), split each line then send it to InputTbl as a row
Public Sub Update_Table(InputTbl As DataTable, InputFile As String)
InputTbl.Rows.Clear()
Dim lines() As String
Dim vals() As String
lines = File.ReadAllLines(InputFile)
For i = 0 To lines.Length - 1
vals = lines(i).ToString().Split("|")
Dim row(vals.Length - 1) As String
For j = 0 To vals.Length - 1
row(j) = vals(j).Trim()
Next j
InputTbl.Rows.Add(row)
Next i
End Sub
I set the table as Data Source for the DGV by DGV.DataSource = Tbl_Sample
The Datatable is created as Follow
Public Sub Sample_Table()
'Method For creating database file
Create_DBFile("Sample.db")
Try
Tbl_Sample.Columns.Add("ID", Type.GetType("System.Int32"))
Tbl_Sample.Columns.Add("Name", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Username", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Account_Type", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Date", Type.GetType("System.String"))
Tbl_Sample.Columns.Add("Time", Type.GetType("System.String"))
Catch ex As Exception
End Try
Update_Table(Tbl_Sample, "Sample.db") 'populate table
End Sub
The way I create columns is not the best. I just copied it from my old program
Solution link here
Imports System.Reflection
Public Sub EnableDoubleBuffered(ByVal dgv As DataGridView)
Dim dgvType As Type = dgv.[GetType]()
Dim pi As PropertyInfo = dgvType.GetProperty("DoubleBuffered", _
BindingFlags.Instance Or BindingFlags.NonPublic)
pi.SetValue(dgv, True, Nothing)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
EnableDoubleBuffered(MyDataGridView, True)
End Sub

VB.NET DataSet table data empty

I'm trying to use the dataset for a report, but the data is gone when I try to use it. Here is my code for the most part:
Variables:
Dim ResultsDataView As DataView
Dim ResultsDataSet As New DataSet
Dim ResultsTable As New DataTable
Dim SQLQuery As String
Search:
This is where a datagrid is populated in the main view. The data shows up perfectly.
Private Sub Search(Optional ByVal Bind As Boolean = True, Optional ByVal SearchType As String = "", Optional ByVal SearchButton As String = "")
Dim SQLQuery As String
Dim ResultsDataSet
Dim LabelText As String
Dim MultiBudgetCenter As Integer = 0
SQLQuery = "A long and detailed SQL query that grabs N rows with 7 columns"
ResultsDataSet = RunQuery(SQLQuery)
ResultsTable = ResultsDataSet.Tables(0)
For Each row As DataRow In ResultsTable.Rows
For Each item In row.ItemArray
sb.Append(item.ToString + ","c)
Response.Write(item.ToString + "\n")
Response.Write(vbNewLine)
Next
sb.Append(vbCr & vbLf)
Next
'Response.End()
If Bind Then
BindData(ResultsDataSet)
End If
End Sub
Binding Data:
I think this is a cause in the issue.
Private Sub BindData(ByVal InputDataSet As DataSet)
ResultsDataView = InputDataSet.Tables("Results").DefaultView
ResultsDataView.Sort = ViewState("SortExpression").ToString()
ResultsGridView.DataSource = ResultsDataView
ResultsGridView.DataBind()
End Sub
Reporting action:
This is where I am trying to use the table data. But it is showing as nothing.
Protected Sub ReportButton_Click(sender As Object, e As EventArgs) Handles ReportButton.Click
For Each row As DataRow In ResultsTable.Rows
For Each item In row.ItemArray
Response.Write(item.ToString)
Next
Next
End Sub
The reason I'm trying to loop through this data is to both display the data in a gridview on the main view as well as export the data to CSV. If there is a different way to export a SQL query to CSV, I'm open to any suggestions.
There has to be something I can do to get the data from the SQL query to persist through the ReportButton_Click method. I've tried copying the datatable, I've tried global variables, I've tried different methods of looping through the dataset. What am I missing?!
Thank you all in advance.
EDIT
Here is the Page_Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
'set focus to postback control
Dim x As String = GetPostBackControlName(Page)
If Len(x) > 0 Then
x = x.Replace("$", "_")
SetFocus(x)
End If
End If
If Not IsPostBack Then
ResultsGridView.AllowPaging = False
'Enable Gridview sorting
ResultsGridView.AllowSorting = True
'Initialize the sorting expression
ViewState("SortExpression") = "ID DESC"
'Populate the Gridview
Search()
End If
End Sub
In your search function add this line after the ResultsTable setting
ResultsTable = ResultsDataSet.Tables(0)
Session("LastSearch") = ResultsTable
Then in your report click event handler recover your data from the Session variable
Protected Sub ReportButton_Click(sender As Object, e As EventArgs) Handles ReportButton.Click
ResultsTable = DirectCast(Session("LastSearch"), DataTable)
For Each row As DataRow In ResultsTable.Rows
For Each item In row.ItemArray
Response.Write(item.ToString)
Next
Next
End Sub
You need to read about ASP.NET Life Cycle and understand that every time ASP.NET calls your methods it creates a new instance of your Page class. Of course this means that global page variables in ASP.NET are not very useful.
Also consider to read about that Session object and not misuse it.
What is the difference between SessionState and ViewState?

Reading to a file line by line multiple choice quiz vb.net

I'm trying to create a multi choice quiz that will read questions and answers from a text file and if the RadioButton selected by the user contains text the same as the answer then the mark will increment by 2. I have tried every single loops and techniques but it either reads the last line only (for loop) or doesn't read the text file at all (do until / do while / while). I need help.
This is what I have done so far
Imports System.IO
Public Class Form5
Dim easy As Boolean
Dim medium As Boolean
Dim difficult As Boolean
Dim easytest As New System.IO.StreamReader("he.txt")
Dim lineseasy() As String = IO.File.ReadAllLines("he.txt")
Dim mediumtest As New System.IO.StreamReader("hm.txt")
Dim linesmedium() As String = IO.File.ReadAllLines("hm.txt")
Dim difficulttest As New System.IO.StreamReader("hd.txt")
Dim fulldata As String
Dim mark As Integer = 0
Private Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Label2.Text = Form3.ComboBox1.Text
Label3.Text = Form3.ComboBox2.Text
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
easytest.Read()
Dim counter As Integer = 0
Dim answer As String
While (easytest.Peek() <> -1)
Dim items() As String = lineseasy(1).Split(",")
Label4.Text = items(0)
RadioButton1.Text = items(1)
RadioButton2.Text = items(2)
answer = Str(3)
End While
End Sub
End Class
Why do you want a textfile where if u use Access database or sql database, it will be very simple .
For example, you create a db in either sql or access, create a table in it, create 3 columns, 1st column for question id,2nd column for question and the last one for the answer.Now add your question id(any random alpha-numeric/any string value),questions and answers in specific cells one by one. Now in you vb.net app,add a label(make it invisible) that will contain the question id, add reference to System.Data.SqlClient(for sql database) or System.Data.OleDb(for access). Now use the following code to check if the answer is correct or not ..
'add this code to all 4 radio buttons
Private Sub RadioBtn1_CheckedChanged
'create your connection string
connectionstring.open
Dim cmd as new SqlCommand("Select * from [table name from database-remove brackets if required] where [question id]=#quid and [answer]=#ans",connectionstring) 'use OleDbCommand of access db
cmd.parametres.add("#quid",sqldbtype.varchar).value=label1.text 'use oledbtype for access db
cmd.parametres.add("#ans",sqldbtype.varcha).value=radiobutton1.text 'when adding this code to radio button2, chage radiobutton1.text with radiobutton2.text , when using it in radio button 3 do the same
Dim adapter As New SqlDataAdapter(cmd) ' use oledbdataadapter for access
Dim table As New DataTable
adapter.Fill(table)
If table.Rows.Count() <= 0 Then
msgbox("WRONG ANSWER")
Else
marktext.text = mark.text+2 ' to increase point by 2
'what ever you want to do when the answer is correct
end if
Now, i hope u know how to load the question/question id(i mean the data) from the database...if you don't that please leave a reply and i'll add them

Datagridview with combobox cannot fill in

Have datagridview which contains 4 columns created manually in datagridview creator. 1st, 2nd and 3rd columns are just textbox columns but the last one is marked as comboboxcolumn. On my form i have button, when user clicks it new row is add to datagridview. For first three columns data is filled up from some variables, and the last combobox column should be filled up for user from datasource so he could select his value out of it. The problem is i have problems with this combobox column and so far couldn't fill it in. This is my actual code
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.Items.AddRange(dt.AsEnumerable().Select(Function(s) s.Field(Of String)("Name")).ToArray())
try
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
Tried also this:
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.DataSource = dt
cbo.ValueMember = "Id"
cbo.DisplayMember = "Name"
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
In both cases every time user clicks button row is added to datagrid but last column's combobox is empty. How to solve that?
Try to add this line for asociate your comboBox to the wanted column :
dgvMaterials.Columns.Insert(3, cbo)

Populating Listbox with Data from DataGridView

I've got 2 forms. Form A has a listbox and a Combobox. Form B has a DataGridView. In my A, my combobox is meant to represent groups for a task. e.g. Inbox, Important etc. So whenever I select an item from the comobobox e.g. I select the "Inbox" Item the DataGridViewer will sort all the rows which contain "Inbox" in one of the columns. This all works fine. I can view the sorted data.
To Load my data into the DataGridView I use:
Dim ds As DataSet
Dim dataset1 As New DataSet("datasetTasks")
Dim table1 As New DataTable("tableTask")
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ds = CreateDataset()
frm_Tasks.DataGridView1.DataSource = ds.Tables("tableTask")
LoadFromXMLfile("C:\Users\Beta4K\Documents\Tasks.FILE")
For Each dr As DataRow In ds.Tables(0).Rows
ListBox1.Items.Add(dr("TaskName").ToString())
Next
End Sub
Private Sub LoadFromXMLfile(filename As String)
If System.IO.File.Exists(filename) Then
Dim xmlSerializer As XmlSerializer = New XmlSerializer(ds.GetType)
Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
ds = CType(xmlSerializer.Deserialize(readStream), DataSet)
readStream.Close()
frm_Tasks.DataGridView1.DataSource = ds.Tables("tableTask")
Else
MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
End If
End Sub
Private Function CreateDataset() As DataSet
table1.Columns.Add("TaskID")
table1.Columns.Add("TaskName")
table1.Columns.Add("TaskMessage")
table1.Columns.Add("TaskDate")
table1.Columns.Add("TaskTime")
table1.Columns.Add("TaskGroup")
dataset1.Tables.Add(table1)
Return dataset1
End Function
Here's the code for my Combobox:
ListBox1.Items.Clear()
Dim dv As New DataView(ds.Tables("tableTask"))
dv.RowFilter = "TaskGroup = '" + ComboBox1.SelectedItem + "'"
frm_Tasks.DataGridView1.DataSource = dv.ToTable("tableTask")
For Each dr As DataRow In ds.Tables(0).Rows
ListBox1.Items.Add(dr("TaskName").ToString())
Next
What this is meant to do is that it clears the listbox, and then reloads the items into the listbox from reading all the data in the DataGridViewer. Since it's already sorted all it has to do is add the items but it doesn't. Instead it just adds all the items regardless of the filter.
Can someone help me.
You loop over the datatable while you need to loop over the dataview
For Each dr As DataRowView In dv
ListBox1.Items.Add(dr("TaskName").ToString())
Next