populate textbox and picture box when row in listview selected - vb.net

Hi i'm currently developing a system(vb.Net). here's the scenario, i a have textbox, picture box and a listview, where when i click a row from the listview it will populate the textbox and a picture box(e.g Users Details and her/his ID picture), but when i click the next row, only the textbox will change its value but the image on picturebox will not change.. here's my partially working code:
Try
If lvUsrProf.SelectedItems.Count > 0 Then
''Call userImage()
Dim adptr As New MySqlDataAdapter
Dim tbl As DataTable
Dim commbuild As MySqlCommandBuilder
'Dim uID As Integer
adptr.SelectCommand = cmd
tbl = New DataTable
adptr = New MySqlDataAdapter("SELECT pic_image FROM userprofile", uCon)
commbuild = New MySqlCommandBuilder(adptr)
adptr.Fill(tbl)
Dim lb() As Byte = tbl.Rows(0).Item("pic_image")
Dim lstr As New System.IO.MemoryStream(lb)
pboxID.Image = Image.FromStream(lstr)
lstr.Close()
txtIdNo.Text = lvUsrProf.SelectedItems(0).SubItems(0).Text
txtName.Text = lvUsrProf.SelectedItems(0).SubItems(1).Text
txtLstNam.Text = lvUsrProf.SelectedItems(0).SubItems(2).Text
cboDept.Text = lvUsrProf.SelectedItems(0).SubItems(3).Text
txtPOs.Text = lvUsrProf.SelectedItems(0).SubItems(4).Text
txtCont.Text = lvUsrProf.SelectedItems(0).SubItems(5).Text
txtEmail.Text = lvUsrProf.SelectedItems(0).SubItems(6).Text
'pboxID.SizeMode = PictureBoxSizeMode.CenterImage
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
uCon.Close()
End Try
Your brilliant ideas and suggestions will be much appreciated!.. Maraming Salamat!!

Looks like you are fetching all images from the table userprofile. Then for every selection you make you select the first item in that result. This image will always be the same since you aren't using a where clause in your select. Try adding something like this:
WHERE ID = lvUsrProf.SelectedItems(0).SubItems(0).Text
This should only fetch you one image and it should be the one connected to the selected account.

Related

How can I add datarow to table multiple time as user define in a textbox?

I have created a datatable in my vb.net project and the datatable is the datasourse of a datagridview.
when a user click on the button called btnAdd, datarow will be added to datatable. I could do it as below for a one datarow. but there is another textbox called "txtQuantity" for item quantity and the user can type any number in it. So my question is, how can I add the same datarow to the datatable number of times as user mention in the txtQuantity ?
Dim dr As DataRow = dtOrderingItem.NewRow
dr("ItemCode") = clckdItemCode
dr("ItemName") = clickdItemName
dr("ServiceCode") = srvc
dr("RatePerItem") = txtItemRate.Text
dtOrderingItem.Rows.Add(dr)
dgvCart.datasource=dtOrderingItem
Use Integer.TryParse() to convert the value in your TextBox to an integer, then use that in a For loop as suggested by SSS.
Dim number As Integer
If Integer.TryParse(txtQuantity.Text, number) Then
If number >= 1 Then
Dim dr As DataRow = dtOrderingItem.NewRow
dr("ItemCode") = clckdItemCode
dr("ItemName") = clickdItemName
dr("ServiceCode") = srvc
dr("RatePerItem") = txtItemRate.Text
For i As Integer = 1 To number
dtOrderingItem.Rows.Add(dr)
Next
dgvCart.datasource = dtOrderingItem
End If
End If

Adding records (Row/ Column) to DataGridView; Collection already belongs to a DataGridView control VB.NET

I'm looking for some help with an issue I am having.
I have multiple text files, in a folder. The folder can have an "unlimited" amount of text files in it, although typically 2-150 files
http://gyazo.com/5f314d1ca374abf9f813914609dd931d (images for this + below, can't embed due to lack of reputation)
Each file then contains an "unlimited" (although typically 0-20 lines) amount of data inside it. Line 1 being the "test number" and line 2 being the "test result" - as seen in the image above
My Data Grid View has 3 columns in it (Username, test number, test result) - as seen in the image above
When running my code below; first record being added to the Data Grid View works perfectly, but the second (or any record after the first) the error occurs:
http://gyazo.com/0d492b97d918853e62c55ee314f3f181 (image) or error message:
"System.InvalidOperationException: Collection already belongs to a
DataGridView control. This operation is no longer valid. at
System.Windows.Forms.DataGridViewCellCollection.Add(DataGridViewCelldataGridViewCell)
at SpellingBee.TeacherMultiResults.TeacherMultiResults_Load(Object
sender, EventArgs e)"
This error occurs on the DGVRow.Cells.Add(DGVCell) 'add cell to row
One thing I don't want to do is change any of my text file/ folder structures, even though I'm aware that it is currently inefficient storage.
How can I fix this error? I'm using VB.net (Visual Studios 2013)
If you need any more information, please just ask
Many thanks.
Imports System.Windows.Forms
Imports System.IO
Public Class TeacherMultiResults
Dim AmountOfFiles
Dim LineCount As Integer = 0
Dim FileName As IO.FileInfo
Dim DGVRow As New DataGridViewRow
Dim DGVCell As DataGridViewCell
Dim Username As String = ""
Dim TestNumber As Integer = 0
Dim TestResult As Integer = 0
Private Sub TeacherMultiResults_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim directory As New IO.DirectoryInfo(LoadForm.CurrentDirectory & "\UserResults\") 'selects directory
Dim FileNames As IO.FileInfo() = directory.GetFiles()
Dim Files As IO.FileInfo
For Each Files In FileNames 'list the names of all files in the specified directory
Username = Files.ToString
Username = (Username.Substring(0, Username.Length - 4)) 'removes the .txt from the name
Try
LineCount = File.ReadAllLines(LoadForm.CurrentDirectory & "\UserResults\" & Username & ".txt").Length 'amount of lines in file
If LineCount > 1 Then
Dim Information As New System.IO.StreamReader(LoadForm.CurrentDirectory & "\UserResults\" & Username & ".txt") 'opens file
LineCount = LineCount / 2 'halfs line count
For i = 0 To LineCount - 1
TestNumber = Information.ReadLine() 'reads line to variable
TestResult = Information.ReadLine() 'reads line to variable
i = i + 1 'adds one to i
DGVCell = New DataGridViewTextBoxCell 'create a new DGV text box cell
DGVCell.Value = Username 'add value to DGV text box cell
DGVRow.Cells.Add(DGVCell) 'add cell to row
DGVCell = New DataGridViewTextBoxCell 'create a new DGV text box cell
DGVCell.Value = TestNumber 'add value to DGV text box cell
DGVRow.Cells.Add(DGVCell) 'add cell to row
DGVCell = New DataGridViewTextBoxCell 'create a new DGV text box cell
DGVCell.Value = TestResult 'add value to DGV text box cell
DGVRow.Cells.Add(DGVCell) 'add cell to row
DGV_MultiResults.Rows.Add(DGVRow) 'add row to DGV
Next
Information.Close() 'Close read
End If
Catch ex As Exception 'if file won't read
MsgBox(ex.ToString) 'show error
Exit Try
End Try 'end of try
Next 'end of files
End Sub
I hope that somebody can help me with this issue, I understand it's fairly simple to fix most probably although I can't seem to find a solution to the error!
Many thanks, Toby.
When working with a DataGridView, it is generally easier to just build an object list and then bind it to the DGV using the built-in data binding features.
For your example, it would look like this:
' Read the files, build objects as you go and add them to a list
Dim lst = New List(Of TeacherMultiResults)
' Replace this part with your file reading code...
Dim t1 = New TeacherMultiResults With {.Username = "Bob", .TestNumber = 1, .TestResult = 75}
lst.Add(t1)
Dim t2 = New TeacherMultiResults With {.Username = "Rick", .TestNumber = 1, .TestResult = 85}
lst.Add(t2)
Dim t3 = New TeacherMultiResults With {.Username = "Zoe", .TestNumber = 1, .TestResult = 95}
lst.Add(t3)
' Bind the list to the DataGridView
Dim bindList = New BindingList(Of TeacherMultiResults)(lst)
Dim bindSrc = New BindingSource
bindSrc.DataSource = bindList
grid.DataSource = bindSrc
Your grid should display each item of your list.

How to get child datagrid values

How can I save values in child popup DataGridView?
I am having parent datagridview and child DataGridView in vb.net
Consider I have 2 columns and 2 rows.
When I click 1st column, I will get child DataGridView just near to this cell. Child DataGridView also has 2 rows and 2 columns where I can enter values.
When I click 2nd column, I'll get another new child DataGridView near this cell.
Now if I move back to first column, values I entered are lost. How can I save entered values in popup child window?
Here is my code:
sub cell_click Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location Dim _pointGrid As Point = DgV.Location Dim _pointLocation As Point _pointLocation.X = _pointCell.X 'width _pointLocation.Y = _pointCell.Y 'height SelectionInGrid() mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y))) end sub
Public SelectionInGrid() Dim t1,t2 As New DataGridViewTextBoxColumn() Dim gv As New DataGridView
gv.Columns.Add(t1)
gv.Columns.Add(t2)
gv.Columns(0).HeaderText = "Employee"
gv.Columns(1).HeaderText = "Currency"
gv.Width = t1.Width + t2.Width
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mControlHost.Padding = Padding.Empty
mControlHost.AutoSize = False
mPopup = New ToolStripDropDown()
mPopup.Padding = Padding.Empty
mPopup.Items.Add(mControlHost)
End
sub cell_click
Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
Dim _pointGrid As Point = DgV.Location
Dim _pointLocation As Point
_pointLocation.X = _pointCell.X 'width
_pointLocation.Y = _pointCell.Y 'height
SelectionInGrid()
mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y)))
end sub
Public SelectionInGrid()
Dim t1,t2 As New DataGridViewTextBoxColumn()
Dim gv As New DataGridView
gv.Columns.Add(t1)
gv.Columns.Add(t2)
gv.Columns(0).HeaderText = "Employee"
gv.Columns(1).HeaderText = "Currency"
gv.Width = t1.Width + t2.Width
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mControlHost.Padding = Padding.Empty
mControlHost.AutoSize = False
mPopup = New ToolStripDropDown()
mPopup.Padding = Padding.Empty
mPopup.Items.Add(mControlHost)
End
The problem lies in the SelectionInGrid Sub which is called every single time you click a cell. In this function you have these lines:
Dim gv As New DataGridView
Dim mControlHost As ToolStripControlHost = New ToolStripControlHost(gv)
mPopup = New ToolStripDropDown()
This means that everytime cell_click runs a new DataGridView, a new ToolStripControlHost and a new ToolStripDropDown are created. To solve this you need to keep track of the different ToolStripDropDowns. For example using a dictionary:
Private PopUps As New Dictionary(Of String, ToolStripDropDown)
sub cell_click
Dim _pointCell As Point = Me.DgV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True).Location
Dim _pointGrid As Point = DgV.Location
Dim _pointLocation As Point
_pointLocation.X = _pointCell.X 'width
_pointLocation.Y = _pointCell.Y 'height
If PopUps.ContainsKey(<Parent Selected cell identifyer>) Then
mPopup = PopUps(<Parent Selected cell identifyer>)
Else
SelectionInGrid()
PopUps.Add(<Parent Selected cell identifyer>,mPopup)
End If
mPopup.Show(DgV.PointToScreen(New Point(_pointLocation.X, _pointLocation.Y)))
end sub
I do believe this should work. "Parent Selected cell identifyer" must be something unique from the Parent DGV row/cell that was clicked.

Clear the DataGridView without loosing the source

I have a form with DataGridView, dataSourced by dataTable. When i insert the record at the end i press the Clear button or form all fields (textboxes, comboboxes, DGV checkboxes) get cleared automatically when i press the save button.
I normally clear the textboxes and comboboxes by writing:
textbox1.text = ""
combobox1.text = ""
but when i do clear DGV with this like:
dgvPurchase.dataSource = nothing
dgvPurchase.Refresh()
or
dgvPurchase.rows.clear()
It does clear the DGV but when i add new entry so DataGridView and data both do not appear into DGV. On the Form Load event I have the following code:
dtField = retrieveFull("ProductBasicInfo")
cmbPurProdID.DisplayMember = "ProdName"
cmbPurProdID.ValueMember = "ProdID"
cmbPurProdID.DataSource = dtField
dtPurchase.Columns.Add("ProdID")
dtPurchase.Columns.Add("ProdName")
dtPurchase.Columns.Add("RetailerID")
dtPurchase.Columns.Add("RetailerName")
dtPurchase.Columns.Add("UnitPrice")
dtPurchase.Columns.Add("Qty")
dtPurchase.Columns.Add("Amount")
dgvPurchase.DataSource = dtPurchase
Please guide me that is there any way that my DataSource do not disturb or if I clear it so it also refresh the DataSource.
This is the add button code, which adds the data into DataGridView
Try
For a As Integer = 0 To dtPurchase.Rows.Count - 1
If dtPurchase.Rows(a).Item("ProdID") = cmbPurProdID.SelectedValue Then
MessageBox.Show("This product is already enlisted")
Exit Sub
End If
Next
dRow = dtPurchase.NewRow
dRow.Item("ProdID") = cmbPurProdID.SelectedValue
dRow.Item("ProdName") = cmbPurProdID.Text
dRow.Item("RetailerID") = cmbPurRetailerID.SelectedValue
dRow.Item("RetailerName") = cmbPurRetailerID.Text
dRow.Item("UnitPrice") = txtPurUnitPrice.Text.Trim
dRow.Item("Qty") = txtPurQty.Text.Trim
dRow.Item("Amount") = txtPurAmount.Text.Trim
dtPurchase.Rows.Add(dRow)
You can create new empty DataTable and set it like this:
c#
DataTable emptyDT = new DataTable();
dgvPurchase.DataSource = emptyDT
vb.Net
Dim emptyDT As New DataTable()
dgvPurchase.DataSource = emptyDT
Does this work for you?

populate combo box by consuming web service returning a dataset

I am trying to populate a combo list box with the results of a web service. The web service returns a dataset with two columns. I want to display column one to the user and capture column two with the user selection of an item from the combox.
right now I can display the first column and capture the selection.
Not sure how to add the connection of column two, to the combo box and capture the selection.
any help would be greatly appreciated!!
Code so far....
myDataSet1 = proxy3.listSuppLang()
Dim x As Integer
Dim dt As DataTable = myDataSet1.Tables(0)
Dim myString As String = ""
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
'some code
For x = 0 To dt.Rows.Count - 1
myString = dt.Rows(x).Item(0)
ComboBox1.Items.Add(myString)
Next
ComboBox1.Visible = True
End If
I found this to work adquately....
If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then
'some code
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "textFieldcolumnName"
ComboBox1.ValueMember = "valueFieldcolumnName"
ComboBox1.Visible = True
End If