Better way to update ComboBox dynamically? - vb.net

I was wondering if anyone had a suggestion or tip to improve the following function of my code. Basically I'm making a Update/Modify Form which fills its fields from a table of a DataBase, everything so far works great until I arrive at the comboboxes.
I have three of them, role, status and gender. The problem is that when I loaded the current user's information like this:
Dim lstDatos As New ArrayList()
lstDatos = gestorUsuario.consultarUsuario(idUsuario)
txtNombre1.Text = lstDatos(0)
txtNombre2.Text = lstDatos(1)
txtApellido1.Text = lstDatos(2)
txtApellido2.Text = lstDatos(3)
cmbGenero.DisplayMember = lstDatos(4) 'HERE
txtCorreo.Text = lstDatos(5)
txtCedula.Text = lstDatos(6)
txtTelefono.Text = lstDatos(7)
cmbRol.Text = lstDatos(8) 'HERE
cmbEstado.Text = lstDatos(9) 'And Here
Only the user's current Rol, Estado and Gender could be selected, to workaround that I made the following:
Private Sub updateRol(sender As Object, e As EventArgs) Handles cmbRol.Click
actualizarComboBox()
End Sub
Which calls the actualizarFunction:
Private Sub actualizarComboBox()
cmbGenero.Items.Add("Masculino")
cmbGenero.Items.Add("Femenino")
cmbEstado.DataSource = gestorUsuario.consultarEstados
cmbEstado.DisplayMember = "nombre_estado"
cmbEstado.ValueMember = "id_estado"
cmbRol.DataSource = gestorRol.consultarRoles
cmbRol.DisplayMember = "nombre"
cmbRol.ValueMember = "id_rol"
End Sub
It kinda works to be honest but...the first time the comboBox is selected you can see a visible jump when it loads the other options, also when I click the combo it automatically loses the "placeholder" if you will, of the user's current information and goes right up to the first option.
So if I have:
*B
As my user's current cmbRol.Text and I click the ComboBox it flickers a bit and changes to:
*A
-B
-C
Where * represent the selected or highlighted option.
If anyone had any suggestions or tips I would be very grateful. Thanks a lot.

Replace
cmbGenero.DisplayMember = lstDatos(4) 'HERE
cmbRol.Text = lstDatos(8) 'HERE
cmbEstado.Text = lstDatos(9) 'And Here
for this:
cmbGenero.SelectedItem = lstDatos(4) 'SelectedItem is sufficient because cmbGenero is not binding to Data Base
cmbRol.SelectedValue = lstDatos(8) 'SelectedValue is required because cmbRol and cmbEstado is binding to Data Base
cmbEstado.SelectedValue = lstDatos(9)

Related

How do I display an image from a DataGridView to another PictureBox in a new Windows Form?

What I'm trying yo do is once I clicked on a row in my DataGridView, clicked on the view button, and it opens a new window that displays all the data from the DataGridView to the textboxes. I already did the first part, but I don't know how to do it with the images (I have 2 image columns). Can someone help me out? I'm only starting, sorry. This is my code for the view button that opens up another window. The picture boxes are always blank.
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
If GunaDataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 0 Then
data.txtID.Text = GunaDataGridView1.CurrentRow.Cells(0).Value.ToString
data.txtLN.Text = GunaDataGridView1.CurrentRow.Cells(1).Value.ToString
data.txtFN.Text = GunaDataGridView1.CurrentRow.Cells(2).Value.ToString
data.txtMN.Text = GunaDataGridView1.CurrentRow.Cells(3).Value.ToString
data.txtGen.Text = GunaDataGridView1.CurrentRow.Cells(4).Value.ToString
data.txtNum.Text = GunaDataGridView1.CurrentRow.Cells(5).Value.ToString
data.txtDOB.Text = GunaDataGridView1.CurrentRow.Cells(6).Value.ToString
data.txtAddress.Text = GunaDataGridView1.CurrentRow.Cells(7).Value.ToString
data.txtPlate.Text = GunaDataGridView1.CurrentRow.Cells(8).Value.ToString
data.txtVT.Text = GunaDataGridView1.CurrentRow.Cells(9).Value.ToString
data.txtVB.Text = GunaDataGridView1.CurrentRow.Cells(10).Value.ToString
data.txtVYM.Text = GunaDataGridView1.CurrentRow.Cells(11).Value.ToString
data.txtSP.Text = GunaDataGridView1.CurrentRow.Cells(12).Value.ToString
data.ownerPhoto.Image = GunaDataGridView1.CurrentRow.Cells(13).Value
data.carPhoto.Image = GunaDataGridView1.CurrentRow.Cells(14).Value
data.ShowDialog()
End If
End Sub
I saw another guy asked this question and another guy commented with this code line:
Dim bytes As Byte() = DataGridView1.CurrentRow.Cells(13).Value
Using ms As New MemoryStream(bytes)
ownerPP.Image = Image.FromStream(ms)
End Using
Dim bit As Byte() = DataGridView1.CurrentRow.Cells(14).Value
Using memory As New MemoryStream(bit)
carPhoto.Image = Image.FromStream(memory)
End Using
It works perfectly. I hope this helps someone out too.

How do you reference data from different formview (ASP.NET)

I'm a still new to the whole creating a website please understand and explain in simple terms.
Ok, so there's a formview called FormView1 and another formview called User. In FormView1 I have a data called CoinEarn and the other formview data called CurrentCoin (from different formview)
I want to update CurrentCoin through a session so I could put it on my update SQL (by Session("CurrentCoin") = CurrentCoin + CoinEarn) after clicking a button (which is placed inside FormView1 template). In the code I gave the button the command name "complete".
Since CurrentCoin is from a different SQL and formview I need to reference/call up the specific data (CurrentCoin) so I can make the session.
I'm not sure if my thought/solution on this problem is correct or not (by calling up data or make a session to update CurrentCoin) but the main objective is to make CurrentCoin += CoinEarn after clicking a button.
Please help me! I've been trying to find the answer but it's too complicated to understand in proper websites.
Protected Sub FormView1_ItemCommand(sender As Object, e As FormViewCommandEventArgs) Handles FormView1.ItemCommand
If e.CommandName = "complete" Then
Dim row As FormViewRow = FormView1.Row
Dim TaskstatusLabel As Label = CType(row.FindControl("TaskStatusLabel"), Label)
Dim what As String = TaskstatusLabel.Text
Dim TaskCoin As Label = CType(row.FindControl("CoinEarnLabel"), Label)
Dim Coin As String = TaskCoin.Text
''Session("CurrentcoinSession") = Coin + CurrentCoin
Dim TaskstatusLabelID As Label = CType(row.FindControl("TaskIDLabel"), Label)
Dim whatTaskID As String = TaskstatusLabelID.Text
Session("TaskIDSession") = whatTaskID
If TaskstatusLabel.Text = "Complete" Then
Session("TaskStatusSession") = "Incomplete"
ElseIf TaskstatusLabel.Text = "Incomplete" Then
Session("TaskStatusSession") = "Complete"
End If
SqlDsStatus1.Update()
FormView1.DataBind()
End If
End Sub
Use of session CurrentCoin optional:
Session("CurrentCoin") = ((Label)MyFormView.FindControl("lblCurrentCoin")).Text
Session("CurrentcoinSession") = Coin + Session("CurrentCoin")
get values from a form view

VB There is no row at position 0

I have a database that currently has nothing in it and I'm trying to create and save the first record, but end up getting an error that says "There is no row at position 0."
What I've done is instead of having a textbox that allows a user to select a shipper or receiver name, I've pulled those out and put in comboboxes that I've bound to the appropriate field in two other databases (shippers.mdb and receivers.mdb).
So, on clicking the "save" button, I want to save the selecteditems (saved to strings) from those comboboxes to each column cell in the database that I want. However, the system isn't letting me save the record at all. I've been trying to figure out how to save the information for the "current" record being edited or entered so for some reason I've decided to use (0), thinking that might work but it seems that isn't right. Here is the code:
Private Sub Button16_Click_1(sender As System.Object, e As System.EventArgs) Handles Button16.Click
Me.OrdersDataSet.orders(0).orderstatus = ComboBox13.SelectedItem.ToString()
shipper1 = ComboBox8.SelectedItem.ToString()
Me.OrdersDataSet.orders(0).SHIPPER1 = shipper1
RECEIVER1 = ComboBox9.SelectedItem.ToString()
Me.OrdersDataSet.orders(0).RECEIVER1 = RECEIVER1
billtoacct = ComboBox7.SelectedItem.ToString()
Me.OrdersDataSet.orders(0).BILLTOACCT = billtoacct
driverassigned = ComboBox10.SelectedItem.ToString()
Me.OrdersDataSet.orders(0).DRIVERASSIGNED = driverassigned
truckassigned = ComboBox11.SelectedItem.ToString()
Me.OrdersDataSet.orders(0).TRUCKASSIGNED = truckassigned
trailerassigned = ComboBox12.SelectedItem.ToString()
Me.OrdersDataSet.orders(0).TRAILERASSIGNED = trailerassigned
Me.Validate()
Me.OrdersBindingSource.EndEdit()
Me.TableAdapterManager12.UpdateAll(Me.OrdersDataSet)
End Sub
What should I be using instead because I can't seem to find something to let me save (or substitute) the combo box items for the current record? I did check in the table adapter that the CRUD is there and I went through to make sure it's set up to fill, update, etc.
Thanks for having a look.
Give this a shot.... PUT ALL OF THIS IN YOUR BUTTON CLICK EVENT AND CHANGE AS NEEDED...
Dim customerOrders As DataSet = New DataSet("CustomerOrders")
Dim ordersTable As DataTable = customerOrders.Tables.Add("Orders")
'ADD YOUR COLUMNS YOU NEED TO...'
ordersTable.Columns.Add("OrderQuantity", Type.GetType("System.Int32"))
ordersTable.Columns.Add("CompanyName", Type.GetType("System.String"))
'AND WHATEVER ELSE YOU NEED TO ADD, CHANGE AS NEEDED'
' Add YOUR ROWS to those columns for the datatable.
ordersTable .Rows.Add(25, "Indocin")
ordersTable .Rows.Add(50, "Enebrel")
ordersTable .Rows.Add(10, "Hydralazine")
ordersTable .Rows.Add(21, "Combivent")
ordersTable .Rows.Add(100, "Dilantin")
Then you have your dataset with the table and your data to set to your table adapter manager
Thanks!

How to reference controls located on different Tabs (VB.NET)

I have an application written in VB.NET that reads data from a file and displays the data on the screen.
Depending on the data in the file, the program has a TabControl with up to 3 tabs and each tab in turn has a DataGridView for displaying data. For example I have a TabControl that has a tab called "Saturday" and a tab called "Sunday".
The problem I am having is that when I read data from a file, the program displays all the data on the Saturday's tab grid because I am not sure how to reference the Grid on the Sunday tab.
To add the DataGridView I am using the following code:
Grid = New DataGridView
Grid.Dock = DockStyle.Fill
Grid.Name = "Grid" & TabControl.SelectedIndex
Grid.Tag = "Grid" & TabControl.SelectedIndex
And this is how I am reading the data in:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
If reader.Name = "cell" Then
y = y + 1
Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
End If
What I almost want to do is something like (pseudocode):
SelectedTab.Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
However when I use the above code it complains:
'Grid' is not a member of 'String'
I hope you understand the issue. Let me know if you need clarification
Your code is a little unclear. However, it appears to me that the following line:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
is creating at least one problem. It looks like you are attempting to refer to a Tabpage control by the string representation of its name, but unless I missed something, what that line is actually doing is trying to make a tabpage control type("SelectedTab") refer to a string type. If that is the case, then you will want to try this instead:
If reader.GetAttribute("controltype") = "Tab" Then
TabControl1.SelectedTab = TabControl1.TabPages(reader.name)
End If
It is a little hard to tell from the code you have posted, but that might get you headed down the right path.
++++++++++++
UPDATE: It appears from your code that you are naming each DGV control by appending the index of the tab on which it is located to the string "grid." I am going to assume that you are using a class member variable named "SelectedTab" to represent the current tab selected in the control. I will assume that at the top of your class you have done something like this:
'Form-or-class scoped memebr variables:
Private SelectedTab As TabPage
Private SelectedGrid As DataGridView
You should be able to refer to the active grid control using something like this:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
' Set SelectedTab member variable to refer to the new selected tab page:
SelectedTab = TabControl1.SelectedTab
' Set the SelectedGrid to refer to the grid control hosted on the selected tab page:
SelectedGrid = TabControl1.SelectedTab.Controls("Grid" & TabControl1.SelectedIndex.ToString())
End Sub
From here, you should be able to use the member variable for SelectedGrid to refer to the grid present on which ever tab page is selected in your tab control.
It is challenging to address your concerns with only fragments of your code. If you have additional difficulties, please post more of your code, so we can better see what else is going on.
Hope that helps!
Okay, I would go about something like this. Maybe you can simply use a DataSet to load the XML data in one line (if they have been saved with DataSet.WriteXML before).
Dim ds As New DataSet
Dim p As TabPage
Dim gv As DataGridView
ds.ReadXml("F:\testdata.xml")
For i As Integer = TabControl1.TabPages.Count - 1 To 0 Step -1
TabControl1.TabPages.RemoveAt(i)
Next
For Each dt As DataTable In ds.Tables
p = New TabPage(dt.TableName)
gv = New DataGridView
' ... configure the gv here...
gv.AutoGenerateColumns = True
gv.Dock = DockStyle.Fill
' ...
gv.DataSource = dt
TabControl1.TabPages.Add(p)
p.Controls.Add(gv)
Next

should changing the parent of a listbox change the selected index?

You wouldn’t think so, but it does when the listbox is bound to a datasource (as far as I can see).
I’ve reduced the behaviour to the code below. The "if" line toggles between loading a list via data binding and loading a list “manually” (both use the same data table). In each case I set the selected index afterwards, and then change the parent form. With manual loading, the selected index is retained, with binding it is lost. I cannot see how this makes any sense – I don't see why changing the host form should alter any property of the list. Is this a bug?
Public Class Form1
Sub main() Handles Me.Load
Dim ListControl1 As ListBox = New ListBox
ListControl1.Parent = Me
Dim dt = New DataTable
dt.Columns.Add("intColourID")
dt.Columns.Add("strName")
dt.Rows.Add({1, "Red"})
dt.Rows.Add({2, "Green"})
dt.Rows.Add({3, "Blue"})
ListControl1.ValueMember = dt.Columns(0).ColumnName
ListControl1.DisplayMember = dt.Columns(1).ColumnName
If False Then
ListControl1.DataSource = dt
Else
For i = 0 To dt.Rows.Count - 1
ListControl1.Items.Add(dt.Rows(i)("strName").ToString)
Next
End If
ListControl1.SelectedIndex = 2
Dim z As Form = New Form
ListControl1.Parent = z
z.Show()
End Sub
End Class
The correct way of adding a control to a form is not to set its parent, but to add it to the Controls collection of the form. If I do it like this I do not get an exception (the three last lines commented out as you write in your comment).
Me.Controls.Add(ListControl1) ' Instead of ListControl1.Parent = Me