none
WPF extract data from DataSet RRS feed

Answers

  • Hi Tan Th,

    From your description, I notice the issue you are experiencing is that how to set values to the controls. I suggest you to use WPF Data Binding.WPF provides a simple and powerful way to auto-update data between the business model and the user interface. This mechanism is called DataBinding. Everytime when the data of your business model changes, it automatically reflects the updates to the user interface and vice versa. This is the preferred method in WPF to bring data to the user interface.I suggest you to use this method.

    Here is something more about Data Binding.

    http://msdn.microsoft.com/en-us/library/ms752057.aspx.

    Here is Coding.

     <StackPanel x:Name="sp1">
       <TextBox Text="{Binding Path=ID}" Margin="5"></TextBox>
       <TextBox Text="{Binding Path=Name}" Margin="5"></TextBox>
       <TextBox Text="{Binding Path=Age}" Margin="5"></TextBox>
     </StackPanel>
    string sql="select ID,Name,Age from Persons"
    SqlDataAdapter da = new SqlDataAdapter(sql, sqlserver_connectionstring);
    DataTable dt=new DataTable();
    da.Fill(dt);
    sp1.DataContext=dt;
    Wish to Help you.

    • Marked as answer by TAN TH Friday, July 26, 2013 10:54 PM
    Friday, July 26, 2013 7:16 AM

All replies

  • Hi Tan Th,

    From your description, I notice the issue you are experiencing is that how to set values to the controls. I suggest you to use WPF Data Binding.WPF provides a simple and powerful way to auto-update data between the business model and the user interface. This mechanism is called DataBinding. Everytime when the data of your business model changes, it automatically reflects the updates to the user interface and vice versa. This is the preferred method in WPF to bring data to the user interface.I suggest you to use this method.

    Here is something more about Data Binding.

    http://msdn.microsoft.com/en-us/library/ms752057.aspx.

    Here is Coding.

     <StackPanel x:Name="sp1">
       <TextBox Text="{Binding Path=ID}" Margin="5"></TextBox>
       <TextBox Text="{Binding Path=Name}" Margin="5"></TextBox>
       <TextBox Text="{Binding Path=Age}" Margin="5"></TextBox>
     </StackPanel>
    string sql="select ID,Name,Age from Persons"
    SqlDataAdapter da = new SqlDataAdapter(sql, sqlserver_connectionstring);
    DataTable dt=new DataTable();
    da.Fill(dt);
    sp1.DataContext=dt;
    Wish to Help you.

    • Marked as answer by TAN TH Friday, July 26, 2013 10:54 PM
    Friday, July 26, 2013 7:16 AM
  • hi herro8,

    with Data Binding, I managed to get some output results to my WPF TextBox.

    Say, I want the input to come from the WPF TextBox too, how do I do that?

    e.g with this query:

    SELECT        ProductID, ProductName, SupplierID, CategoryID
    FROM            Products
    WHERE        (CategoryID = 8)

    I want the CategoryID to get its input from the WPF TextBox, is that possible?

    Code is as below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Data.SqlClient;
    
    namespace NorthwindProductEditor
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                SqlConnection();
            }
    
            private void SqlConnection()
            { 
                SqlConnection cn;
                SqlCommand cmd;
                string MyString;
    
                MyString="Select * from Products";
                cn = new SqlConnection("Data Source=TanThiamHuat-PC;Initial Catalog=Northwind;Integrated Security=True"); 
                cmd=new SqlCommand(MyString,cn);
                cn.Open();
                MessageBox.Show("Connected");
                cn.Close();
    
                //string sql = "select ID,Name,Age from Persons";
                //SqlDataAdapter da = new SqlDataAdapter(sql, cn);
               // DataTable dt = new DataTable();
               // da.Fill(dt);
               // sp1.DataContext=dt;
    
                //string sql = "select ProductID,ProductName,SupplierID from Products";
               
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                NorthwindDataSetTableAdapters.ProductsTableAdapter productsTableAdapter1;
                productsTableAdapter1 = new NorthwindDataSetTableAdapters.ProductsTableAdapter();
    
                NorthwindDataSet.ProductsDataTable newProductsTable;
                newProductsTable = productsTableAdapter1.GetData();
                sp1.DataContext = newProductsTable;
            }
        }    
    }
    
    <Window x:Class="NorthwindProductEditor.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <StackPanel x:Name="sp1">
                <TextBox Text="{Binding Path=CategoryID}" Margin="5" Width="110"></TextBox>
                <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
                <TextBox Text="{Binding Path=ProductName}" Margin="5" Width="111"></TextBox>
                <TextBox Text="{Binding Path=SupplierID}" Margin="5" Width="109"></TextBox>
            </StackPanel>
        </Grid>
    </Window>
    
    thanks

    Saturday, July 27, 2013 12:36 AM
  • I want the CategoryID to get its input from the WPF TextBox, is that possible?

          Yes,we can do it.

         WPF data binding supports the following binding source types:

    Binding Source

    Description

    common language runtime (CLR) objects

    You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.

    For more information about how to implement a class that can serve as a binding source, see Implementing a Class for the Binding Source later in this topic.

    dynamic objects

    You can bind to available properties and indexers of an object that implements the IDynamicMetaObjectProvider interface. If you can access the member in code, you can bind to it. For example, if a dynamic object enables you to access a member in code viasomeObjet.AProperty, you can bind to it by setting the binding path to AProperty.

    ADO.NET objects

    You can bind to ADO.NET objects, such as DataTable. The ADO.NET DataView implements the IBindingList interface, which provides change notifications that the binding engine listens for.

    XML objects

    You can bind to and run XPath queries on an XmlNode, XmlDocument, or XmlElement. A convenient way to access XML data that is the binding source in markup is to use an XmlDataProvider object. For more information, see How to: Bind to XML Data Using an XMLDataProvider and XPath Queries.

    You can also bind to an XElement or XDocument, or bind to the results of queries run on objects of these types by using LINQ to XML. A convenient way to use LINQ to XML to access XML data that is the binding source in markup is to use anObjectDataProvider object. For more information, see How to: Bind to XDocument, XElement, or LINQ for XML Query Results.

    DependencyObjectobjects

    You can bind to dependency properties of any DependencyObject. For an example, see How to: Bind the Properties of Two Controls.

    you can lerarn more about Bind the Properties of Two Controls.

    http://msdn.microsoft.com/en-us/library/ms742167.aspx

    Monday, July 29, 2013 4:52 AM