Tips and tricks for .NET using ASP and VB code.

Get unique values from entire table.

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim DataSet1 As New Data.DataSet
        DataSet1.ReadXml("C:\Temp\DataTable1.xml")
        Dim DataTable1 As Data.DataTable = DataSet1.Tables(0)
        Dim UniqueValuesInDataTable As Data.DataTable = GetUniqueTableValues(DataTable1)
    End Sub

    Protected Function GetUniqueTableValues(ValuesDataTable As Data.DataTable) As Data.DataTable
        Dim ReturnValuesDataTable As Data.DataTable = Nothing
        Dim UniqueColumnValuesDataTable As New Data.DataTable
        UniqueColumnValuesDataTable.Columns.Add("UniqueValues")
        For Each ValuesDataColumn As Data.DataColumn In ValuesDataTable.Columns
            Dim UniqueValuesInThisColumnDataTable As Data.DataTable = ValuesDataTable.DefaultView.ToTable(True, ValuesDataColumn.ColumnName)
            UniqueValuesInThisColumnDataTable.Columns(0).ColumnName = "UniqueValues"
            For Each UniqueValuesDataRow As Data.DataRow In UniqueValuesInThisColumnDataTable.Rows
                UniqueColumnValuesDataTable.ImportRow(UniqueValuesDataRow)
            Next UniqueValuesDataRow
        Next ValuesDataColumn
        UniqueColumnValuesDataTable.DefaultView.Sort = "UniqueValues"
        ReturnValuesDataTable = UniqueColumnValuesDataTable.DefaultView.ToTable(True, "UniqueValues")
        Return ReturnValuesDataTable
    End Function

No comments:

Post a Comment

Search This Blog