Home Forums WPF controls Xceed DataGrid for WPF x:Key and basic data binding questions

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

    I’m new to WPF and C#. I’ve attached the XAML and C# code i’m using at the very end, in case it’s needed. My questions are extremely basic:

    1. how do you change the sample code from tbl “Orders” to point to another tbl in another .mdb database. It seems simple, but I don’t know what x:key=”cvs_orders” refers to (is it the name of the primary key in the table?). Also, does “x:Static Application.Current” ever change, or is it simply a reference to the existence of C# code behind the Xaml form?

    2. How do you bind a LINQ table?

    3. How do you bind a master-detail table (or two or more tables in a master-child relationship)?

    4. Is there any good source for sample code usage in database scenarios for someone new (but including LINQ and SQL Server 2005/2008)?

    Thanks!

    <Window x:Class=”WpfApplication2.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    Title=”Window1″ Height=”521″ Width=”914″ xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”&gt;
    <Grid Height=”406″ Width=”665″>
    <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key=”cvs_orders”
    Source=”{Binding Source={x:Static Application.Current},
    Path=Orders}”/>
    </Grid.Resources>
    <xcdg:DataGridControl x:Name=”OrdersGrid”
    ItemsSource=”{Binding Source={StaticResource cvs_orders}}”/>
    <xcdg:DataGridControl Margin=”28,78,50,84″ Name=”dataGridControl1″ />
    </Grid>
    </Window>

    namespace WpfApplication2
    {
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
    public Window1()
    {
    InitializeComponent();
    }
    static void App()
    {
    DataSet dataSet = new DataSet();
    string mdbFile = @”c:\users\MyUserAccount\MyDatabase.mdb”;
    string connString = String.Format(“Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}”, mdbFile);
    OleDbConnection conn = new OleDbConnection(connString);
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(“SELECT * FROM Orders;”, conn);
    adapter.Fill(dataSet, “Orders”);
    m_orders = dataSet.Tables[“Orders”];
    }
    public static DataTable Orders
    {
    get
    {
    return m_orders;
    }
    }
    private static DataTable m_orders;
    }
    }

    Imported from legacy forums. Posted by M (had 3888 views)

    User (Old forums)
    Member
    Post count: 23064

    I will try to explain the first bits since I know nothing of LINQ.

    In the C# code you specify the DB name in the mdbFile variable, and the data that is retrieved in specified in the adapter.SelectCommand … the SELECT string. The use of the name “Orders” is just that, a name that so happens to contain the contents of the Orders table because of the SELECT statement.

    Using the OleDB stuff you have populated the DataSet and reference that table in the DataTable called m_orders. You have setup a public property called Orders so that you can bind to this Table from XAML.

    In the XAML, the Xceed Grid is pospulated by its ItemsSource, which has a binding to the StaticResource called cvs_orders. Higher up in the XAML Grid.Resources section you have declared a DataGridCollectionViewSource which has the Binding to the Orders property from the C#. To access this inside XAML it is given the key of cvs_orders. Hence this name can be anything and is used to connect these two things together.

    I would suggest going through the Xceed docs and samples – just change teh samples and see their effect as being the best way to find your way around.

    Imported from legacy forums. Posted by Derek (had 448 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you for the patient explanation. That makes sense. Still, I don’t actually see any data in the grid. All I see is one box within another, and both containing the message “drag a column header here to group by that column.” There are, however, no column headers anywhere in the window, so the message is not particularly helpful. I did give the name of an existing Access database in the code, and it has a table called Orders also, so I didn’t have to change that anywhere in the C# code.

    Imported from legacy forums. Posted by M (had 450 views)

    User (Old forums)
    Member
    Post count: 23064

    If you refer to the DataBinding sample code that comes with the Xceed grid, the code that you are trying to get working is in that sample. Refer specifically to the App.xaml.cs (where the DB connection is definied).

    What WPF is telling you in the errors (Debug output) is that it could not find the Orders property to bind to. Message is [System.Windows.Data Error: 35 : BindingExpression path error: ‘Orders’ property not found on ‘object’ ”App’ (HashCode=1867017)’. BindingExpression:Path=Orders; DataItem=’App’ (HashCode=1867017); target element is ‘DataGridCollectionViewSource’ (HashCode=16906910); target property is ‘Source’ (type ‘Object’)]

    Simplest solution is to move the DB definition code from your Window to the App.xaml.cs file of your project as:

    public partial class App : System.Windows.Application
    {
    protected override void OnStartup(StartupEventArgs e)
    {
    m_orders = PopulateData();

    base.OnStartup(e);
    }

    DataTable PopulateData()
    {
    DataSet dataSet = new DataSet();
    string mdbFile = @”c:\Northwind.mdb”;
    string connString = String.Format(“Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}”, mdbFile);
    OleDbConnection conn = new OleDbConnection(connString);
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand = new OleDbCommand(“SELECT * FROM Orders;”, conn);
    adapter.Fill(dataSet, “Orders”);
    return dataSet.Tables[“Orders”];
    }

    public static DataTable Orders
    {
    get
    {
    return m_orders;
    }
    }
    private static DataTable m_orders;

    }

    This will work for training/learning until you know how to do more.

    Imported from legacy forums. Posted by Derek (had 499 views)

    User (Old forums)
    Member
    Post count: 23064

    I think there is some confusion. The code I posted did not generate any errors (i.e. nothing in Debug output). What happens is that the app runs, but no rows appear in the grid. I get a box inside a box, with both boxes saying “Drag a column header here to group by that column.” However, there are no column headers shown, and I can’t figure out how to make rows appear.

    I tried your code, which is identical except that the code was moved behind the app class instead of behind the window1 class. I get the same behavior.

    I’m sure I’m overlooking something very simple, but I don’t know what. Would you happen to have both working XAML and cs that you could post? It may be that I’m overlooking some simple property that would cause the data to show on the datagrid.

    Imported from legacy forums. Posted by M (had 435 views)

    User (Old forums)
    Member
    Post count: 23064

    Never mind my last post, I think I have it working now. Although there were no errors, it looks like the code wasn’t getting saved in the right file. I also discovered that the code won’t work if compiled on x64 machines unless it is set for x86 build only in project properties because the Jet OLEDB driver is an x86-only component.

    Thanks for your help–it did point me in the right direction.

    Imported from legacy forums. Posted by M (had 5214 views)

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