Combobox Make First Row As Display - vb.net

So my combobox is used to populate a datagridview when user clicks the customersID in the combobox it will popupate the grid. I have the code for populating but the problem is that they combobox does no reset itself. It still shows the current value that was Selected. I wanted to know it is posible to reset the combobox after selected index event. The code below is how i got my combobobox.
' after a command and connection was made
customerAdapt = New SqlDataAdapter(customerQuery,con)
customerSet = New DataSet
customerAdapt.Fill(customerSet)
customerCb.DataSource = adoAddtRs.tables(0)
customerCb.DisplayMember = "custID"
customerCb.ValueMember = "custID"
customerCb.Visible = False
customerCb.DropDownStyle = ComboBoxStyle.DropDownList

Try this piece of coding
customerCb.selectedindex = -1

Related

How to access cells from programmatically created datagridview in VB.net?

When creating a datagridview in VB.net I cannot get at the cells from anywhere except the Sub where it was made.
I tried placing the code in Form_Load and tried make the Sub Public.
I use Controls.Find, which finds the datagridview, but I cannot access any rows or cells. I can create textbox and label controls and access their text content from anywhere, but not so with the datagridview. This is odd to me.
Using and learning Visual Basic in Visual Studio 2017, for use in my own home business utility.
Dim dgv As New DataGridView
dgv.Location = New Point(2, 200)
dgv.Size = New Size(300, 50)
dgv.Name = "NewDGV"
Dim ColumnTitles() As String = {"ProdId", "Price"}
Dim ColumnWidths() As Integer = {50, 50)
For i = 0 To UBound(ColumnTitles)
Dim col As New DataGridViewTextBoxColumn
col.DataPropertyName = ColumnTitles(i)
col.HeaderText = ColumnTitles(i)
col.Name = ColumnTitles(i)
col.Width = ColumnWidths(i)
dgv.Columns.Add(col)
Next
Controls.Add(dgv)
‘the following sees the datagridview just fine
‘within the same sub
Dim x = dgv.Rows(0).Cells(0).Value
This is how I am creating this control. Perhaps I am Adding to Controls in the wrong way? I am open to better ways to do this.
Dim dgv As DataGridView = CType(Controls("NewDGV"), DataGridView)
dgv.Rows(0).Cells(0).Value = "I found you!"
What this does is find the first Control in the Controls collection, coerces it into a DataGridView type, and assigns it to the variable dgv. You can than manipulate dgv. In this case I put some text in the first cell on the first row.
You could ask the control collection for the instance of your datagridview
Dim MyDgv As DataGridView = Controls.Cast(Of DataGridView).First(Function(dgv) dgv.Name = "NewDGV")
Then work with "MyDGV" properties, methods, and/or events

combobox default value, vb.net

i have a combobox that populates from a database column but when the form loads, the combobox appears with the first item on the database. the value of this particular combobox determines the value of some other controls on the form so i want the combobox to load with an empty entry. thanks as i'll appreciate any help i can get.
here is my code:
'all variables declared...
connection.Open()
command = New MySqlCommand("SELECT PUMP FROM test.pump", connection)
dataadapter.SelectCommand = command
dataadapter.Fill(dataset)
With Me.PumpComboBox
.DisplayMember = "pump"
.DataSource = dataset
End With
Use
combobox1.SelectedIndex = -1
This sets the value of the combo box to a blank entry.
Maybe adding empty value and then selected that empty value using selectedindex after the combbobox populated will work
In your Form.Load include the following:
ComboBox1.SelectedIndex = 1
Here "ComboBox1" is your ComboBox and "1" equals index you want to be set by default.

Move to row that starts with specific text in DataGridView

I have a text box on my form that when you enter a customer code into it, my DataGridView will highlight the row in the grid if an exact match is found (see code below). However, I would like it if the DataGridView would display the first record that is the closest match. For example if a user enters just ‘C’ the grid would go to the first customer code that starts with a C. Please note I do not want to filter the DataGridView to achieve this. All records must remain in it.
Dim targetString As String = txtAccountCode.Text
For Each row As DataGridViewRow In CustomerDataGridView.Rows
If row.Cells(0).Value = targetString Then
CustomerDataGridView.ClearSelection()
CustomerDataGridView.Rows(row.Index).Selected = True
CustomerDataGridView.FirstDisplayedScrollingRowIndex =
CustomerDataGridView.SelectedRows(0).Index
Dim selectedIndex = CustomerDataGridView.SelectedRows(0).Index
CustomerDataGridView.Rows(selectedIndex).Selected = True
CustomerDataGridView.Rows(selectedIndex).Cells(0).Selected = True
End If
Next

how to retrieve the value as array from a dynamically created textbox using its ID in vb.net?

I am new to this programming method. The case is I have created the checkbox and textbox dynamically where the name of the text box and check box is equal and it is handled using database.
The data I need to get is from the textbox which is selected using checkbox by the user.
The details in the array need to be name of the textbox and the value of the textbox
I created the checkbox and textbox using this method. Some thing like this
Dim checkBox = new CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(offset, 10)
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
I successfully created checkbox at run time. Help me getting the value, and suggest me with the suitable array list.
the naming for the check box is number 1 to n values same for the textbox

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