【问题标题】:How to search through a database using WPF and display the results in a DataGrid如何使用 WPF 搜索数据库并在 DataGrid 中显示结果
【发布时间】:2013-04-24 15:20:39
【问题描述】:

我正在尝试从我的数据库中的“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
    }

【问题讨论】:

  • 您的代码容易受到 sql 注入的影响:它实际上是在乞求被黑客入侵。在你做任何其他事情之前,你需要研究如何使用参数化查询。

标签: c# sql wpf winforms


【解决方案1】:

当您使用后面的代码时,您应该提供 DataGrid 的 ItemSouce,但在使用 MVVM 架构处理绑定时,则使用 DataContext

所以替换

PurchaseResults.DataContext = dt;

PurchaseResults.ItemsSource = dt;

【讨论】:

  • 您好,感谢您的回复,我尝试过并收到此错误:错误 1 ​​无法将类型“System.Data.DataTable”隐式转换为“System.Collections.IEnumerable”。存在显式转换(您是否缺少演员表?)
  • 很抱歉再次打扰您,现在当我使用有效条件进行搜索时,我只在数据网格中看到一个白色区域,TitleOfRecord 和 Date 列没有显示
  • 将其AutoGenerateColumns 设置为true
  • 感谢您的帮助,我现在已经完成了大部分工作,谢谢
【解决方案2】:

改变

PurchaseResults.DataContext = dt;

PurchaseResults.ItemsSource = dt;

【讨论】:

  • 您好,感谢您的回复,我尝试过并收到此错误:错误 1 ​​无法将类型“System.Data.DataTable”隐式转换为“System.Collections.IEnumerable”。存在显式转换(您是否缺少演员表?)
  • 我现在无法在此处尝试,我不确定您为什么会收到此错误。但是您可以尝试使用 dt.DefaultView 而不是 dt 作为 ItemsSource。
【解决方案3】:

您是否定义了数据网格中的列。如果不是,当您将 AutoGenerateColumns 属性设置为 true 时,WPF 会自动为您生成它。

PurchaseResults.AutoGenerateColumns = true;
PurchaseResults.ItemsSource = dt;

【讨论】:

    【解决方案4】:

    你可以试试

    PurchaseResults.ItemsSource = dt.AsDataView();
    

    【讨论】:

    • 您好,输入了代码,但是当我使用有效条件搜索时,我只在数据网格中看到一个白色区域/线条,TitleOfRecord 和 Date 列没有显示
    • @user2316176 : 检查 PurchaseResults.AutoGenerateColumns 和 XAML 中 itemsource 的绑定
    • @user2316176 检查这个 url wpftutorial.net/DataGrid.html 如果你想手动定义列
    猜你喜欢
    • 2014-08-30
    • 2013-02-23
    • 2018-01-15
    • 2015-08-12
    • 2012-02-06
    • 2010-11-24
    • 2017-06-28
    • 1970-01-01
    • 2015-08-05
    相关资源
    最近更新 更多