1

「PurchaseTable」テーブルからデータベースから購入を検索し、結果を DataGrid に表示しようとしています。Windows フォームを使用してそれを行う方法を知っています。

private void SearchButton_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter("SELECT TitleOfRecord, DateOfPurchase FROM  PurchaseTable WHERE (NameOfCustomer = '"+NameOfCustomerSText.Text+"')", connection);
        //Query that will return the Title of the Records and Dates of purchases depending on the Customer which has been searched for
        SDA.Fill(dt);
        PurchaseResults.DataSource = dt;
        //Results will appear in the PurchaseResults DataGrid
    }

しかし、WPFでそれを行う方法がわかりません。これまでのところ、これはありますが機能しません:

private void SearchCustomerButton_Click(object sender, RoutedEventArgs e)
    {
        DataTable dt = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter("SELECT TitleOfRecord, DateOfPurchase FROM  PurchaseTable WHERE (NameOfCustomer = '" + NameOfCustomerSText.Text + "')", connection);
        //Query that will return the Title of the Records and Dates of purchases depending on the Customer which has been searched for
        SDA.Fill(dt);
        PurchaseResults.DataContext = dt;
        //Results will appear in the PurchaseResults DataGrid
    }

私はこれらの名前空間を持っています:

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;
using System.Data;

このコードを使用して問題を解決しました。

private void SearchCustomerButton_Click(object sender, RoutedEventArgs e)
    {
        SqlDataAdapter da;
        DataSet ds;
        da = new SqlDataAdapter("SELECT TitleOfRecord, DateOfPurchase FROM  PurchaseTable WHERE (NameOfCustomer = '" + NameOfCustomerSText.Text + "')", connection);
        //Query that will return the Title of the Records and Dates of purchases depending on the Customer which has been searched for
        ds = new DataSet();
        da.Fill(ds);
        PurchaseResults.ItemsSource = ds.Tables[0].DefaultView;
        //Results will appear in the PurchaseResults DataGrid
    }
4

4 に答える 4

1

データ グリッドで列を定義しましたか。そうでない場合は、AutoGenerateColumns プロパティを true に設定すると、WPF によって自動的に生成されます。

PurchaseResults.AutoGenerateColumns = true;
PurchaseResults.ItemsSource = dt;
于 2013-04-24T15:27:59.500 に答える
1

変化する

PurchaseResults.DataContext = dt;

PurchaseResults.ItemsSource = dt;
于 2013-04-24T15:22:32.950 に答える
1

あなたが試すことができます

PurchaseResults.ItemsSource = dt.AsDataView();
于 2013-04-24T15:43:34.523 に答える