【问题标题】:Displaying SQL results in Datagrid WPF在 Datagrid WPF 中显示 SQL 结果
【发布时间】:2017-06-28 04:01:02
【问题描述】:

我正在开发一个 WPF 应用程序,它在 DataGrid 中显示 SQL 查询结果。到目前为止,我得到了一个空白网格。我的代码如下:

<DataGrid HorizontalAlignment="Left" Width="990" Margin="0,0,0,1">
    <DataGrid Name="dataGrid1" AutoGenerateColumns="True" />
</DataGrid>

这是网格的代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        FillDataGrid();
    }

    private void FillDataGrid()
    {
        string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        string CmdString = string.Empty;
        using (SqlConnection con = new SqlConnection(ConString))
        {
            CmdString = "SELECT BatchNo, FormulaId, Description, CONVERT(VARCHAR(19), SchStartDate, 101) SchStartDate, CONVERT(VARCHAR(19), SchCompletionDate, 101) SchCompletionDate, BatchStatus, CONVERT(VARCHAR(19), ActStartDate, 101) ActStartDate, CONVERT(VARCHAR(19), ActCompletionDate, 101) ActCompletionDate, ProcessCellId, Notes FROM mf1.dbo.BM_View_SL_ProdBatches WHERE BatchStatus != 'Closed' AND SchCompletionDate < convert(varchar(19), GETDATE(), 101) AND BatchStatus != 'X' ORDER BY SchStartDate";
            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGrid1.ItemsSource = dt.DefaultView;
        }
    }
}

【问题讨论】:

  • 除了DataGrid 中有DataGrid 之外,只要您的查询返回数据,您的代码确实没有问题。
  • 你能告诉我我在哪里复制吗?
  • 在你的 XAML 中......
  • 看看我的样本。显然我使用了不同的数据表。
  • 谢谢,我知道这很愚蠢。

标签: c# sql wpf xaml datagrid


【解决方案1】:

您的XAML 应如下所示:

<Window x:Class="WpfApplication375.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication375"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
    <DataGrid Name="dataGrid1" AutoGenerateColumns="True" />
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2018-11-12
    • 2014-07-05
    相关资源
    最近更新 更多