Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
470
Bind Column ValueList directly to a DataTable
posted

Hi,

I have a hierarchical grid, which is bound to a dataset via the DataBind method.

On certain columns I have to display the text in stead of numbers.

I do this at the moment in creating valuelists for these columns by looping thru the rows of a datatable.

Somtimes I need the same valuelists in two columns but different bands or even in two grids on the same form.

To get this working I have to clone the valulists otherwise I get a runtime error.

I'm wondering if there is no way to bind a valuelist of acolumn directly to the dataset.

 

 

 

 

Parents
No Data
Reply
  • 460
    posted

    i'v built this function with 4 overloads, you pass a dt it returns as valuelist

    Public Overloads Shared Function getLookupValueList(ByVal lkpdt As DataTable, ByVal displayCol As String, ByVal isExpr As Boolean) As ValueList

    Return getLookupValueList(lkpdt, displayCol, isExpr, String.Empty, False)

    End Function

     

    Public Overloads Shared Function getLookupValueList(ByVal lkpDT As DataTable, ByVal displayCol As String, ByVal isExpr As Boolean, ByVal filter As String) As ValueList

    Return getLookupValueList(lkpDT, displayCol, isExpr, filter, False)

    End Function

     

    Public Overloads Shared Function getLookupValueList(ByVal lkpDT As DataTable, ByVal displayCol As String, ByVal isExpr As Boolean, ByVal sort As Boolean) As ValueList

    Return getLookupValueList(lkpDT, displayCol, isExpr, String.Empty, sort)

    End Function

    Public Overloads Shared Function getLookupValueList(ByVal lkpDT As DataTable, ByVal displayCol As String, ByVal isExpr As Boolean, ByVal filter As String, ByVal sort As Boolean) As ValueList

    'same as above, but it filters the datatable by the given filter value

    If isExpr Then

    lkpDT.Columns.Add("expr", GetType(String), displayCol)

    displayCol = "expr"

    End If

    Dim myRows As DataRow()

    If filter = String.Empty Then

    If sort = True Then

    myRows = lkpDT.Select("TRUE=TRUE", displayCol)

    Else

    myRows = lkpDT.Select()

    End If

    Else

    If sort = True Then

    myRows = lkpDT.Select(filter, displayCol)

    Else

    myRows = lkpDT.Select(filter)

    End If

    End If

    Dim myVL As New ValueList

    myVL.Key = lkpDT.TableName

    For Each row As DataRow In myRows

    If Not row.RowState = DataRowState.Deleted Then myVL.ValueListItems.Add(row.Item(0), row.Item(displayCol).ToString)

    Next

    If myVL.ValueListItems.Count = 0 Then

    myVL.ValueListItems.Add(0, "**No Items In List**")

    End If

     

     

    If isExpr Then

    lkpDT.Columns.Remove("expr")

    End If

    Return myVL

    End Function

Children