Home Forums WPF controls Xceed DataGrid for WPF SQL database binding

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #24367 |

    Hi,

    I’m trying to bind a database data, but I’m not successful. Could you help me?

    XAML file:
    <Window x:Class=”WpfPositions.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid&#8221;
    Title=”WpfPositions” Height=”421″ Width=”605″>
    <Grid ShowGridLines=”False”>
    <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key=”positions”
    Source=”{Binding Source={x:Static Application.Current}, Path=TablePositions}”/>
    </Grid.Resources>

    <Grid.RowDefinitions>
    <RowDefinition Height=”20″/>
    <RowDefinition/>
    <RowDefinition/>
    <RowDefinition Height=”20″/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width=”10″/>
    <ColumnDefinition/>
    <ColumnDefinition Width=”100″/>
    <ColumnDefinition Width=”10″/>
    </Grid.ColumnDefinitions>
    <xcdg:DataGridControl Grid.Column=”1″ Grid.Row=”1″ x:Name=”mainGridView” ItemsSource=”{Binding Source={StaticResource positions}}”/>
    <StackPanel Grid.Column=”2″ Grid.Row=”1″>
    <Button Grid.Column=”2″ Grid.Row=”2″ Height=”25″ Margin=”5,5,5,5″ Name=”queryButton” Click=”queryButton_OnClick” Content=”SQL Query”/>
    <Button Grid.Column=”2″ Grid.Row=”2″ Height=”25″ Margin=”5,5,5,5″ Name=”button5″ Content=”Another action”/>
    </StackPanel>
    </Grid>
    </Window>

    Code behind:
    public partial class Window1 : System.Windows.Window
    {
    private SqlDataAdapter dAdapterPositions;
    private SqlDataAdapter dAdapterFills;
    private SqlDataAdapter dAdapterTrades;
    private static DataTable tablePositions;
    private static DataTable tableFills;
    private static DataTable tableTrades;
    private DataSet dSet;

    public static DataTable TablePositions
    {
    get
    {
    return tablePositions;
    }
    }
    public static DataTable TableFills
    {
    get
    {
    return tableFills;
    }
    }
    public static DataTable TableTrades
    {
    get
    {
    return tableTrades;
    }
    }
    public DataSet DSet
    {
    get
    {
    return dSet;
    }
    }

    public Window1()
    {
    InitializeComponent();
    }

    public void queryButton_OnClick(object sender, RoutedEventArgs e)
    {
    string connString = “Data Source=192.168.3.11;Initial Catalog=BOS_DEV;Trusted_Connection=True;”;
    SqlConnection dbConnection = new SqlConnection(connString);

    dSet = new DataSet();

    dAdapterPositions = new SqlDataAdapter();
    dAdapterPositions.SelectCommand = new SqlCommand(“SELECT * FROM ARSNettedPositions”, dbConnection);
    dAdapterPositions.Fill(dSet, “Positions”);
    tablePositions = dSet.Tables[“Positions”];

    dAdapterFills = new SqlDataAdapter();
    dAdapterFills.SelectCommand = new SqlCommand(“SELECT * FROM ARSFillsHeader”, dbConnection);
    dAdapterFills.Fill(dSet, “Fills”);
    tableFills = dSet.Tables[“Fills”];

    dAdapterTrades = new SqlDataAdapter();
    dAdapterTrades.SelectCommand = new SqlCommand(“SELECT * FROM ARSTradesHeader”, dbConnection);
    dAdapterTrades.Fill(dSet, “Trades”);
    tableTrades = dSet.Tables[“Trades”];

    mainGridView.DataContext = dSet.Tables[“Positions”];
    }

    It looks fine to me, but there must be some error. If you see it, please help, thanks.

    Imported from legacy forums. Posted by Lukas (had 598 views)

    Xceed Support
    Member
    Post count: 5658

    Have you installed VS2008 beta 2?

    If the answer is yes, then please use the following:

    <xcdg:DataGridControl Grid.Column=”1″ Grid.Row=”1″ x:Name=”mainGridView” ItemsSource=”{Binding Source={StaticResource positions}<b>, Path=View</b>}”/>

    If not, please let me know.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 199 views)

    User (Old forums)
    Member
    Post count: 23064

    The answer is no.

    I have VS2005 (Version 8.050727.762 (SP.050727-7600)).
    +
    Extesions for Windows WF
    Orcas Technology Preview – .NET Framework 3.0 Development Tools 1.0

    That says my ‘About … ‘

    And, I’m using Datagrid for WPF 1.2

    Imported from legacy forums. Posted by Lukas (had 430 views)

    Xceed Support
    Member
    Post count: 5658

    Looked at your code and noticed that you are assigning the “Positions” table to the DataGridControl’s DataContext, but the DataGridControl’s ItemsSource is bound to a StaticResource who is itself bound to the “TablePositions” property of the Application.Current.

    Does the TablePositions property exists on your Application class?

    Imported from legacy forums. Posted by Marcus [Xceed] (had 233 views)

    User (Old forums)
    Member
    Post count: 23064

    Yes, you are right. The TablePositions property does not exist in Application class. It is defined in Window1 class as a static property. Not sure if static is needed (I was following some example I think).

    I’ve tried add namespace
    xmlns:src=”clr-namespace:WpfPositions”

    and change the binding source
    <xcdg:DataGridCollectionViewSource x:Key=”positions”
    Source=”{Binding Source={x:Static src:Window1.TablePositions}}”/>

    but it not work properly. (throws exception ‘Cannot create instance of Window1’)

    Could you please help me, with this simple piece of code?
    Or it is all wrong by design?

    Dou you know any good databinding online tutorial with examples?
    Thanks a lot.

    Imported from legacy forums. Posted by Lukas (had 4401 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.