Sorting in Generic List(T)

  1. I will create Customer object



    Class Customer
    Private _Name As String
    Private _Age As Integer

    Public Property Name() As String
    Get
    Return _Name
    End Get
    Set(ByVal value As String)
    _Name = value
    End Set
    End Property
    Public Property Age() As Integer
    Get
    Return _Age
    End Get
    Set(ByVal value As Integer)
    _Age = value
    End Set
    End Property
    End Class



  2. Add two compare methods , one for compare by name, and one for compare by age



    Class Customer
    Private _Name As String
    Private _Age As Integer

    Public Property Name() As String
    Get
    Return _Name
    End Get
    Set(ByVal value As String)
    _Name = value
    End Set
    End Property
    Public Property Age() As Integer
    Get
    Return _Age
    End Get
    Set(ByVal value As Integer)
    _Age = value
    End Set
    End Property

    Public Shared Function CompareByName(ByVal c1 As Customer, _
    ByVal c2 As Customer) As Integer
    Return c1.Name.CompareTo(c2.Name)
    End Function
    Public Shared Function CompareByAge(ByVal c1 As Customer, _
    ByVal c2 As Customer) As Integer
    Return c1.Age.CompareTo(c2.Age)
    End Function

    End Class



  3. Create a Collection with generic List(T)



    Dim CustList As New List(Of Customer)

    cus1 = New Customer()
    cus1.Name = "B"
    cus1.Age = 23
    CustList.Add(cus1)

    cus2 = New Customer()
    cus2.Name = "A"
    cus2.Age = 24
    CustList.Add(cus2)

    cus3 = New Customer()
    cus3.Name = "C"
    cus3.Age = 27
    CustList.Add(cus3)



  4. To use the List.Sort(Generic Comparison) create a instance of Comparison delegate and register it with CompareByName and CompareByAge method.



    Dim comp1 As New Comparison(Of Customer)(Customer.CompareByName)
    CustList.Sort(comp1)


    For Each cus As Customer In CustList
    Console.WriteLine((cus.Name & " ") + cus.Age)
    Next

    Dim comp2 As New Comparison(Of Customer)(Customer.CompareByAge)
    CustList.Sort(comp2)

    For Each cus As Customer In CustList
    Console.WriteLine((cus.Name & " ") + cus.Age)
    Next



RowUpdating not firing!!!

I create a simple function to update some data in the GridView.

And I add a Edit button, when the edit button is clicked in a GridView control, the row is redisplayed in edit mode:

<asp:CommandField ButtonType="Image"
EditImageUrl="~/images/i_edit.gif"
ShowEditButton="True" />


In edit mode, with the data available in editable controls such as TextBox and CheckBox controls. The Edit button becomes an Update or Save button, and when users click it, the RowUpdating event will be firing.

when I finish my code, when I click the "Update link", the RowUpdating event is not firing!!!
I try to debug it, yes, it is totally not firing the RowUpdating event. I dont know what happen?!?!

Finally, I found the problem, Which is because I set the ButtonType="Image", I want used an icon become a button, and I also set EditImageUrl="~/images/i_edit.gif", but I leave the UpdateImageUrl blank!?!?! so that why it cannot call the RowUpdating event.

<asp:CommandField ButtonType="Image"
EditImageUrl="~/images/i_edit.gif"
UpdateImageUrl="~/images/i_save.gif"
CancelImageUrl="~/images/i_cancel.png"
ShowEditButton="True" />

In DataGridView, Change the cell value use my own date format.

I have a datagridview with a column populated from a datetime field. I would like to change the date format to "31/Dec/2009" (no follow the server date format).

I used the following code to display the date, it is work when I test in my PC.

<asp:boundfield headertext="Join Date" datafield="joindate" dataformatstring="{0:dd/MMM/yyyy}" >

but after I upload to the web server, it cannot work!

After research, a setting in the grid columns - HtmlEncode="false" and it should be used when the formats as specifified in the dataformat string.

<asp:boundfield headertext="Join Date" datafield="joindate" dataformatstring="{0:dd/MMM/yyyy}" htmlencode="False"> </asp:boundfield>

How to use ReadXml? (direct read from a string xml)

Normally, we used the ReadXml to read a xml file.
but if I already have a xml in a string, ReadXml cannot directly read the xml into dataset/datatable!!!

The following example first creates a simple dataset by a xml string:

1. we has a xml string


sXml+="<root>"
sXml+=" <func>"
sXml+=" <function>Testing</function>"
sXml+=" </func>"
sXml+="</root>"



2. We convert the string into byte, and write into memory stream.

Dim myEncoder As New System.Text.ASCIIEncoding
Dim bytes As Byte() = myEncoder.GetBytes(sXml.ToString)
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(bytes)


3. We declare the dataset first.

Dim dsRoot As New DataSet("root")
Dim dtNew As DataTable = New DataTable("func")
dtNew.Columns.Add(New DataColumn("function", GetType(String)))
dsRoot.Tables.Add(dtNew)


4. Now, we can read the xml from the memory stream into the dataset.

dsRoot.ReadXml(ms)

Null values convert to 0 - Crystal Report

When there are some null values in the "Running Total Fields" column, i should print 0(Zero);
I try first method:

IIF(not isnull({#RPay}),{#RPay},0)

but it cannot work.

Finally, I change it like the following code:

currencyVar cTotal := 0;
if not isnull({#RPay}) then cTotal := cTotal + {#RPay};
cTotal