WPF Databinding - Community Credit Forums
in

    Community Credit Forums

WPF Databinding

Last post Wed, Dec 17 2008 11:45 PM by Happyarimaan. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • Wed, Dec 17 2008 7:55 AM

    WPF Databinding

    Iam trying to bind data to a WPF datagrid.

     

    My relevant codebehind code is as under :

    Private Sub BindData()

    Dim conn As SqlConnection = New SqlConnection

    Dim command As SqlCommand

    Dim dataadp As SqlDataAdapter = New SqlDataAdapter

    Dim dtTbl As Data123DataSet = New Data123DataSet

    Dim query As String = "SELECT EntityCode, Title, Name FROM dbo.Entity"

    Dim connstring As String = "Data Source=SRITANU;Initial Catalog=Data123;User ID=sa"

    conn = New SqlConnection(connstring)

    Try

    command = New SqlCommand(query, conn)

    conn.Open()

    dataadp.SelectCommand = commanddataadp.Fill(dtTbl, "Entity")

    DataContext = dtTbl

    Catch ex As Exception

    conn.Close()

    End Try

    End Sub

     

    The XAML CODE IS AS UNDER :

     

    <Window x:Class="Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="670" xmlns:c1grid="http://schemas.componentone.com/wpf/C1DataGrid" WindowState="Maximized">

    <Grid Width="574">

    <Grid.ColumnDefinitions>

    <ColumnDefinition Width="574*" />

    <ColumnDefinition Width="0*" />

    </Grid.ColumnDefinitions>

    <c1grid:C1DataGrid Margin="0,12,0,21" DataContext="{Binding dtTbl}" Name="C1DataGrid1" ItemsSource="{Binding Path=Data123DataSet.dtTbl, ElementName= Window, Mode=Default}">

    <c1grid:C1DataGrid.Columns>

    <c1grid:Column Caption="CUSTID" ColumnName="EntityCode" PropertyName="EntityCode " />

    <c1grid:Column Caption="TITLE" ColumnName="Title" PropertyName=" Title"/>

    <c1grid:Column Caption="NAME" ColumnName="Name" PropertyName=" Name"/>

    </c1grid:C1DataGrid.Columns>

    </c1grid:C1DataGrid>

    </Grid>

    </Window>

     

    Please point out where I have gone wrong. The project is running but no data is displayed in the grid

    Regards ......... Happyarimaan

    Be who you are and say what you feel, because those who mind don't matter
    and those who matter don't mind.
  • Wed, Dec 17 2008 8:11 AM In reply to

    Re: WPF Databinding

    To View Data: 

    • You don't need the ElementName in your DataGrid binding.
    • Change ItemsSource to ItemsSource="{Binding}"

    Hint:  When troubleshoot WPF Data Bindings, always run in debug mode and view the Visual Studio Output window for binding exception messages.

     

    Cheers,

    Karl

    Code Project MVP, Karl's Blog : Get Mole v4 Here also!

    Just a grain of sand on the worlds beaches.
  • Wed, Dec 17 2008 9:02 AM In reply to

    • MikeBrown
    • Top 100 Contributor
    • Joined on Tue, Apr 22 2008
    • Indianapolis, IN
    • Posts 5

    Re: WPF Databinding

    In your codebehind you set the DataContext for the window as dtTbl. DataContext inherits down the tree so you don't need to set it again. A databinding by default looks at your data context. So the following should work.

    <c1grid:C1DataGrid Margin="0,12,0,21"  Name="C1DataGrid1" ItemsSource="{Binding}">

    <c1grid:C1DataGrid.Columns>

    <c1grid:Column Caption="CUSTID" ColumnName="EntityCode" PropertyName="EntityCode " />

    <c1grid:Column Caption="TITLE" ColumnName="Title" PropertyName=" Title"/>

    <c1grid:Column Caption="NAME" ColumnName="Name" PropertyName=" Name"/>

    </c1grid:C1DataGrid.Columns>

    </c1grid:C1DataGrid>

    Filed under:
  • Wed, Dec 17 2008 11:45 PM In reply to

    Re: WPF Databinding

    Thanks Karl! Thanks Mike !

     

     I did as instructed and it worked

     

    WHoa

    Regards ......... Happyarimaan

    Be who you are and say what you feel, because those who mind don't matter
    and those who matter don't mind.
Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems