I have a Northwind database and when i want to change or put a new name at a textbox where the names of the ships are, my program to check all the names that already exist in the database and if there is not this name then to show me a message. I created a new table with only the ship names with fill and get commands. I don't know where is my mistake on the code.
Private Sub ShipNameTextBox_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ShipNameTextBox.Validating
For i As Integer = 0 To NorthwindDataSet.Orders1.Rows.Count - 1
If Not (ShipNameTextBox.Text = Convert.ToString(NorthwindDataSet.Orders1.Rows(i))) Then
MessageBox.Show("The boat name should be one of the list")
ShipNameTextBox.Focus()
End If
Next
End Sub
You can use DataTable Select() method like below which will return a DataRow[] (assuming that your datatable have a column name shipname)
DataRow[] rows = NorthwindDataSet.Orders1.Select("shipname = " + ShipNameTextBox.Text.Trim())
Then you can just check if the returned DataRow array has any rows. If filter condition doesn't match them it will be empty
if(rows.Length > 0)
{
MessageBox.Show("The boat name should be one of the list")
ShipNameTextBox.Focus()
}
Related
My project has a DataGridView, in frmConsProd, where the ID column field, receives the prdCod column value from the SQL DB Table.
I need to pass two parameters to second form frmDetailProd when I click on the Consult button,tsbCons. The first parameter is the string "Consult", however, I'm having trouble sending the ID column value of the DataGridView as the second parameter.
Code:
Public Class frmConsProd
Private Sub tsbCons_Click(sender As Object, e As EventArgs) Handles tsbConsult.Click
Try
Using frm As New frmDetailProd("Consult", "datagrid ID value")
frm.txtPrdCod.Enabled = True
frm.txtPrdCod.Text = ""
frm.ShowDialog()
End Using
tsbRefresh.PerformClick()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End Class
How to get the ID value of my DataGridView, to be able to send it as a second parameter in the Consult button tsbCons?
If you are only after one value you can try this:
dim CellValue = yourGrid.Rows(0).Cells(4).value
This example would give you the value of first row, 5th column.
It worked
I created a string and assigned it the value of the datagrid column to be able to pass it as a parameter:
DIM codPrd As String = DataGridView.CurrentRow,Cells("prdCod").Value
As per this item I have added code to a datagridview to automatically add column filtering list to certain column headers.
Private Sub DataGridView1_BindingContextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.BindingContextChanged
For Each col As DataGridViewColumn In DataGridView1.Columns
If col.Name = "rating" Or col.Name = "check" Or col.Name = "artist" Or col.Name = "genre" Or col.Name = "album" Or col.Name = "year" Or col.Name = "bpm" Or col.Name = "cover" Then
col.HeaderCell = New DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell)
End If
Next
End Sub
Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
FilterText = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(DataGridView1)
If String.IsNullOrEmpty(FilterText) Then FilterText = ""
End Sub
However if my database bound column data contains the same string but with different cases the filter list contains multiple entries. For example my data for the datagridview artist column might contain "Abba" and "ABBA" and the datagridview auto generated column header list contains both case versions.
How can I make the list case insensitive without modifying the underlying database data case so I only see "Abba" once in the list. (note I do not create the list as it is generated automatically from the bound datagridview database source data, see the link above)
Although the code provided by Jimi did not resolve the issue it did give me a hint as to where to look.
I needed to expose PopulateFilters() to get to the code where I could manipulate the data being created for the Datagridview column header filter lists. To do this I downloaded the example given in the link above.
From this I added DataGridViewAutoFilter.dll to my project and added the reference "Imports DataGridViewAutoFilter". This allowed me to open the public class DataGridViewAutoFilterColumnHeaderCell which contained the PopulateFilters() sub.
To prevent case duplicates being created in the list I simply created a List Of String
Dim valueitems As New List(Of String)
For Each value As Object In list
If Not valueitems.Contains(value.ToString.ToLower) Then
valueitems.Add(value.ToString.ToLower)
'original PopulateFilters code...
End If
Next value
Obviously there are other methods I could have used to get rid of duplicates however this worked for me.
I have a list box with lots of options (198 to be exact) and they are names of items.
I need to convert each name to be equal to an integer (the item id) so I can write that number to a file.
For example, if they selected the first item, then the integer would be set equal to 3000 but if they picked the second item, it would be 3001 and so on.
I hope you can understand, wasn't sure how to word it. Thanks.
Create an ENUMERATION and assign each item a value such as below
Public Enum MyCountryCodes As Integer
drzCOUNTRY_UNKNOWN = 0
drzCOUNTRY_AFGHANISTAN = 1
drzCOUNTRY_ALBANIA = 2
drzCOUNTRY_ALGERIA = 3
drzCOUNTRY_AMERICANSAMOA = 4
drzCOUNTRY_ANDORRA = 5
... etc etc
drzCOUNTRY_YEMEN = 241
drzCOUNTRY_ZAMBIA = 242
drzCOUNTRY_ZIMBABWE = 243
End Enum
You mean like this ?
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(ListBox1.SelectedIndex + 3000)
End Sub
Assign a dataTable to the ItemSource of your listBox. The DataTable can obviously have more than one field in it. You'll have to configure the properties of the list box to set one of it's fields (the string to display) as visible, then have the other field as visible = false. In one of the events (such as selectedItemChange i think) access the item's SelectedItem that corresponds to the id field.
I'm close to getting this to work, but currently can't get any output to display in the listbox. I had it working, but needed to move some things around to get the join function to work.
In my program, a user enters input into a textbox and an array is displayed in a listbox based on what they type in. For example, if they type in "a", all foods (in the textfile that is connected to the program) that start with "a" will be displayed.
When there is output, I need to find a way to name this array (which is created based on what the user inputs) and join all of the items in the listbox (example: foods stacked on top of each other in the listbox will be shown at the bottom as a string).
I am posting the code that I have thus far; all of the errors that I'm getting (and potentially my logic errors) are just in the first public class until the end of the first if-next statement:
Public Class frmFoods
Dim foods() As String = IO.File.ReadAllLines("foods.txt")
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim Letter As String = txtLetter.Text.ToUpper
Dim smallerarray() As Array
Dim userarray As String
lstOutput.Items.Clear()
If IsNumeric(txtLetter.Text) = False Then
For Each food As String In foods
smallerarray = listfoods(Letter)
lstOutput.Items.Add(Letter)
userarray = Join(smallerarray, ", ")
lstOutput.Items.Add(userarray)
Next
ElseIf IsNumeric(txtLetter.Text) = True Then
MessageBox.Show("Please enter a letter.")
Else
MessageBox.Show("The text box is empty")
End If
End Sub
Function listfoods(ByVal letter As String) As String()
Dim foodarray(foods.Count - 1) As String
Dim counter As Integer = 0
For Each food As String In foods
If food.StartsWith(letter) Then
foodarray(counter) = food
counter += 1
End If
Next
ReDim Preserve foodarray(counter - 1)
Return foodarray
End Function
you need to save the results of the listfoods function in a dictionary or similar and associate it with a key if you want to 'Name' it, although its not clear why you need to do this
As for listing the foods starting with the particular letter, you just need to iterate your result of the function listfoods and separate each one by a comma don't you?
What you have now will create many lists of each food as you are getting the list of food beginning with a particular letter for each food.. As I understand the question you only need to do that once.
Example:
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim Letter As String = txtLetter.Text.ToUpper
Dim smallerarray() As Array
Dim userarray As String
lstOutput.Items.Clear()
If IsNumeric(txtLetter.Text) = False Then
'get all items from the file which begin with the letter the user chose
smallerarray = listfoods(Letter)
'add that letter to the output listbox
lstOutput.Items.Add(Letter)
'join all of the elements which begin with that letter
'into a single comma separated string
userarray = Join(smallerarray, ", ")
'add that string to the output
lstOutput.Items.Add(userarray)
ElseIf IsNumeric(txtLetter.Text) = True Then
MessageBox.Show("Please enter a letter.")
Else
MessageBox.Show("The text box is empty")
End If
End Sub
it would probably be useful for you to step through the code and see the values of the variable at each place and compare this with what you expect to see if you can see where the actual value differs from what your logically expect so you can start to identify where the issue is
I have a gridview0 in a multiview and when I click select on a row I have the GridView0_SelectedIndexChanged sub change to a different view in the multiview, which has a different gridview1 but the same datasource as gridview0. This is when it errors out and it displays the invalid column name error, with the column name being the datakeyname of the first gridview0 row that was selected.
The first image is the view of gridview0, and the second is the error that occurs when I click select. Thanks!
image one http://img291.imageshack.us/img291/9221/gridview0.jpg
image two http://img188.imageshack.us/img188/6586/gridview1.jpg
Protected Sub GridView0_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView0.SelectedIndexChanged
Dim ISTag As String = GridView0.SelectedDataKey.Value.ToString
Dim type As String = getTypeMethod(ISTag)
filterText.Text = type
If (type.Equals("Computer")) Then
InventoryComputer.SelectCommand = "SELECT * FROM T_Computer WHERE ISTag = " & ISTag
MultiView1.ActiveViewIndex = 8
End If
End Sub
Adding a new datasource and setting the where to the original gridview worked.