Bind to an ADO.NETDataSet : Binding « Windows Presentation Foundation « VB.Net






Bind to an ADO.NETDataSet

Bind to an ADO.NETDataSet
     

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:WpfApplication1"
  Title="ADODataSetSample" Loaded="OnInit" Background="White" Height="250" Width="450">
  <StackPanel>
    <StackPanel.Resources>
      <c:IntColorConverter x:Key="MyConverter"/>
      <DataTemplate x:Key="BookItemTemplate">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <TextBlock Text="{Binding Path=Title}" Grid.Column="0"/>
          <TextBlock Text="{Binding Path=ISBN}" Grid.Column="1" />
          <TextBlock Grid.Column="2" Text="{Binding Path=NumPages}" Background="{Binding Path=NumPages,
              Converter={StaticResource MyConverter}}"/>
        </Grid>
      </DataTemplate>
    </StackPanel.Resources>
    <ListBox Name="myListBox" Height="200"
      ItemsSource="{Binding Path=BookTable}"
      ItemTemplate ="{StaticResource BookItemTemplate}"/>
    <Button Click="OnClick">Add Record</Button>
  </StackPanel>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Collections.Generic

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      Me.InitializeComponent()
    End Sub
    Private myDataSet As DataSet
    Private Sub OnInit(sender As Object, e As EventArgs)
      Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\BookData.mdb")
      Dim adapter As New OleDbDataAdapter("SELECT * FROM BookTable;", conn)

      myDataSet = New DataSet()
      adapter.Fill(myDataSet, "BookTable")

      myListBox.DataContext = myDataSet
    End Sub
    Private Sub OnClick(sender As Object, e As RoutedEventArgs)
      Dim myDataTable As DataTable = myDataSet.Tables("BookTable")
      Dim row As DataRow = myDataTable.NewRow()

      row("Title") = "A"
      row("ISBN") = "0-1111-1111-2"
      row("NumPages") = 1
      myDataTable.Rows.Add(row)
    End Sub
  End Class
  Public Class IntColorConverter
    Implements IValueConverter
    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
      Dim numValue As Integer = CInt(value)
      If numValue < 50 Then
        Return System.Windows.Media.Brushes.Green
      Else
        Return System.Windows.Media.Brushes.Red
      End If
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
      Return Nothing
    End Function
  End Class
End Namespace

   
    
    
    
    
  








Related examples in the same category

1.Bind to a Property of a UI ElementBind to a Property of a UI Element
2.Bind a Property of an Element to ItselfBind a Property of an Element to Itself
3.Specify a Default Value for a BindingSpecify a Default Value for a Binding
4.Create a Two-Way BindingCreate a Two-Way Binding
5.Debug Bindings Using Attached PropertiesDebug Bindings Using Attached Properties
6.String Array ResourceString Array Resource
7.Bind animate target value to Canvas(Window) widthBind animate target value to Canvas(Window) width
8.Display Current Date and Time: binding the DateTime.Now to LabelDisplay Current Date and Time: binding the DateTime.Now to Label
9.One way and two way bindingOne way and two way binding
10.Custom Element Binding WindowCustom Element Binding Window
11.Get XmlElement from Bounded viewGet XmlElement from Bounded view
12.Customize UpdateSourceExceptionFilterCustomize UpdateSourceExceptionFilter
13.Assign your own class to DataContent and bind to TextBoxAssign your own class to DataContent and bind to TextBox
14.Async bindingAsync binding
15.Null property bindingNull property binding
16.Binding Property with ExceptionBinding Property with Exception
17.Hierarchical Binding for three level nested objectsHierarchical Binding for three level nested objects
18.BindingOperations.GetBindingExpressionBindingOperations.GetBindingExpression
19.Bind to enum typesBind to enum types
20.Bind to a MethodBind to a Method
21.Manual Update TargetManual Update Target
22.Bind to the Values of an EnumerationBind to the Values of an Enumeration
23.Master Detail BindingMaster Detail Binding
24.Data binding using collections composed of mixed types of data.Data binding using collections composed of mixed types of data.
25.Binding Environment InfoBinding Environment Info
26.Text Data BindingText Data Binding
27.Bind to an Existing Object InstanceBind to an Existing Object Instance
28.Digital ClockDigital Clock
29.Use Data Triggers to Change the Appearance of Bound DataUse Data Triggers to Change the Appearance of Bound Data