Archive

Archive for the ‘VB.NET’ Category

DataTable.DefaultView Property – Gets a customized View of the Table that may include a Filtered View, or a Cursor position.

Gets a customized view of the table that may include a filtered view, or a cursor position.
The DefaultView property returns a DataView you can use to sort, filter, and search a DataTable.

The following example sets a property of the DataTable object’s DataView through the DefaultView property. The example also shows the binding of a DataGridView control to a DataTable named “Suppliers” that includes a column named “CompanyName.”

Private Sub BindDataGrid()
   Dim table As New DataTable

   ' Insert code to populate a DataTable with data.

   ' Bind DataGrid to DataTable
   DataGrid1.DataSource = table
End Sub 

Private Sub ChangeRowFilter()
   Dim gridTable As DataTable = _
       CType(dataGrid1.DataSource, DataTable)

   ' Set the RowFilter to display a company names 
   ' that begin with A through I.
   gridTable.DefaultView.RowFilter = "CompanyName < 'I'"
End Sub



Advertisements
Categories: Database, VB.NET Tags: ,

Dynamicaly create XML Elements in XML Literals

February 6, 2011 Leave a comment

XML Literals

d is a Class that has a Fuction named GetXMLFilenames defined.
GetXMLFilenames returns a List(Of XElements).


Dim _XMLDoc = <OBJECT>
  <OBJTYPE>0</OBJTYPE>
  <PAGES><%= d.PageCount %></PAGES>
  <%= From elem In d.GetXMLFilenames Select
     New XElement(elem.Name, elem.Value)
  %>
</OBJECT>
  
   Public Function GetXMLFilenames() As List(Of XElement)
      Try
         Dim tempstr As String = ""
         Dim _XElementList As New List(Of XElement)

         Dim _i As Integer = 0
         For Each _Filename In _Filenames
            _i += 1
            Dim _XElement As New XElement("FILE" &amp; _i, _Filename)
            _XElementList.Add(_XElement)
         Next
         Return _XElementList

      Catch ex As Exception
         Throw ex
      End Try
   End Function

Categories: LINQ, VB.NET Tags:

Like Operator

Compares a string against a pattern.
Copy

result = string Like pattern

Parts

result

Required. Any Boolean variable. The result is a Boolean value indicating whether or not the string satisfies the pattern.
string

Required. Any String expression.
pattern

Required. Any String expression conforming to the pattern-matching conventions described in “Remarks.”

Remarks

If the value in string satisfies the pattern contained in pattern, result is True. If the string does not satisfy the pattern, result is False. If both string and pattern are empty strings, the result is True.
Comparison Method

The behavior of the Like operator depends on the Option Compare Statement. The default string comparison method for each source file is Option Compare Binary.
Pattern Options

Built-in pattern matching provides a versatile tool for string comparisons. The pattern-matching features allow you to match each character in string against a specific character, a wildcard character, a character list, or a character range. The following table shows the characters allowed in pattern and what they match.

Characters in pattern

Matches in string

?

Any single character

*

Zero or more characters

#

Any single digit (0–9)

[ charlist ]

Any single character in charlist

[! charlist ]

Any single character not in charlist
Character Lists

A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in string and can include almost any character code, including digits.

An exclamation point (!) at the beginning of charlist means that a match is made if any character except the characters in charlist is found in string. When used outside brackets, the exclamation point matches itself.
Special Characters

To match the special characters left bracket ([), question mark (?), number sign (#), and asterisk (*), enclose them in brackets. The right bracket (]) cannot be used within a group to match itself, but it can be used outside a group as an individual character.

The character sequence [] is considered a zero-length string (“”). However, it cannot be part of a character list enclosed in brackets. If you want to check whether a position in string contains one of a group of characters or no character at all, you can use Like twice. For an example, see How to: Match a String against a Pattern (Visual Basic).
Character Ranges

By using a hyphen (–) to separate the lower and upper bounds of the range, charlist can specify a range of characters. For example, [A–Z] results in a match if the corresponding character position in string contains any character within the range A–Z, and [!H–L] results in a match if the corresponding character position contains any character outside the range H–L.

When you specify a range of characters, they must appear in ascending sort order, that is, from lowest to highest. Thus, [A–Z] is a valid pattern, but [Z–A] is not.
Multiple Character Ranges

To specify multiple ranges for the same character position, put them within the same brackets without delimiters. For example, [A–CX–Z] results in a match if the corresponding character position in string contains any character within either the range A–C or the range X–Z.
Usage of the Hyphen

A hyphen (–) can appear either at the beginning (after an exclamation point, if any) or at the end of charlist to match itself. In any other location, the hyphen identifies a range of characters delimited by the characters on either side of the hyphen.
Collating Sequence

The meaning of a specified range depends on the character ordering at run time, as determined by Option Compare and the locale setting of the system the code is running on. With Option Compare Binary, the range [A–E] matches A, B, C, D, and E. With Option Compare Text, [A–E] matches A, a, À, à, B, b, C, c, D, d, E, and e. The range does not match Ê or ê because accented characters collate after unaccented characters in the sort order.
Digraph Characters

In some languages, there are alphabetic characters that represent two separate characters. For example, several languages use the character æ to represent the characters a and e when they appear together. The Like operator recognizes that the single digraph character and the two individual characters are equivalent.

When a language that uses a digraph character is specified in the system locale settings, an occurrence of the single digraph character in either pattern or string matches the equivalent two-character sequence in the other string. Similarly, a digraph character in pattern enclosed in brackets (by itself, in a list, or in a range) matches the equivalent two-character sequence in string.
Overloading

The Like operator can be overloaded, which means that a class or structure can redefine its behavior when an operand has the type of that class or structure. If your code uses this operator on such a class or structure, be sure you understand its redefined behavior. For more information, see Operator Procedures (Visual Basic).

Example: This example uses the Like operator to compare strings to various patterns. The results go into a Boolean variable indicating whether each string satisfies the pattern.
Visual Basic


Dim testCheck As Boolean
' The following statement returns True (does "F" satisfy "F"?)
testCheck = "F" Like "F"
' The following statement returns False for Option Compare Binary
'    and True for Option Compare Text (does "F" satisfy "f"?)
testCheck = "F" Like "f"
' The following statement returns False (does "F" satisfy "FFF"?)
testCheck = "F" Like "FFF"
' The following statement returns True (does "aBBBa" have an "a" at the
'    beginning, an "a" at the end, and any number of characters in
'    between?)
testCheck = "aBBBa" Like "a*a"
' The following statement returns True (does "F" occur in the set of
'    characters from "A" through "Z"?)
testCheck = "F" Like "[A-Z]"
' The following statement returns False (does "F" NOT occur in the
'    set of characters from "A" through "Z"?)
testCheck = "F" Like "[!A-Z]"
' The following statement returns True (does "a2a" begin and end with
'    an "a" and have any single-digit number in between?)
testCheck = "a2a" Like "a#a"
' The following statement returns True (does "aM5b" begin with an "a",
'    followed by any character from the set "L" through "P", followed
'    by any single-digit number, and end with any character NOT in
'    the character set "c" through "e"?)
testCheck = "aM5b" Like "a[L-P]#[!c-e]"
' The following statement returns True (does "BAT123khg" begin with a
'    "B", followed by any single character, followed by a "T", and end
'    with zero or more characters of any type?)
testCheck = "BAT123khg" Like "B?T*"
' The following statement returns False (does "CAT123khg"?) begin with
'    a "B", followed by any single character, followed by a "T", and
'    end with zero or more characters of any type?)
testCheck = "CAT123khg" Like "B?T*"
 



Categories: VB.NET Tags:

Retrieve Class Properties

Get all of the Properties for a Class call Invoices

   Private Function GetInvoiceProperties() as Boolean
         ' List the properties.
         Dim property_value As Object
         Dim properties_info As PropertyInfo() = Invoices(0).[GetType]().GetProperties()

         lvwProperties.BeginUpdate()
         lvwProperties.Items.Clear()
         For i As Integer = 0 To properties_info.Length - 1
            With properties_info(i)
               If .GetIndexParameters().Length = 0 Then
                  property_value = .GetValue(Invoices(RecordNumber - 1), Nothing)
                  If property_value Is Nothing Then
                     ListViewMakeRow(lvwProperties, .Name, "<Nothing>")
                  Else
                     ListViewMakeRow(lvwProperties, .Name, property_value.ToString)
                  End If
               Else
                  ListViewMakeRow(lvwProperties, .Name, "<array>")
               End If
            End With
         Next i
   End Function

   Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal item_title As String, ByVal ParamArray subitem_titles() As String)
      Try
         ' Make the item.
         Dim new_item As ListViewItem = lvw.Items.Add(item_title)

         ' Make the sub-items.
         For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0)
            new_item.SubItems.Add(subitem_titles(i))
         Next i
      Catch ex As Exception

      End Try
   End Sub
Categories: VB.NET Tags: ,

Format DataGridView Cells

Need to format a DataGrid Cell?



   Private Sub grdDataSchema_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles grdDataSchema.CellFormatting
      Try
         If e.ColumnIndex = 1 Then
            If (e.Value.ToString.Contains("H")) Then
               e.CellStyle.BackColor = Color.Red
               e.CellStyle.ForeColor = Color.White
            End If
         End If

      Catch ex As Exception
          Throw ex
      End Try
   End Sub

Categories: VB.NET Tags: ,

Move to Next DataGridView Row via Code

November 21, 2010 6 comments

Dim _rowIndex = grdDataLines.SelectedRows(0).Index + 1
If _rowIndex <= grdDataLines.Rows.Count - 1 Then

Dim nextRow As DataGridViewRow = grdDataLines.Rows(_rowIndex)

' Move the Glyph arrow to the next row
grdDataLines.CurrentCell = nextRow.Cells(0)
grdDataLines.Rows(_rowIndex).Selected = True

End If

Categories: VB.NET Tags:

Join String Function

November 8, 2010 Leave a comment
Dim mArray(2) As String
mArray(0) = "Text1"
mArray(1) = "Text2"
mArray(2) = "Text3" <em>

'Join array.</em>
Dim strTemp As String = String.Join(",", mArray)
Console.WriteLine(strTemp)

Categories: Strings, VB.NET Tags: ,