我正在使用的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;
}
}
}
答案 0 :(得分: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>