1

I have a WPF application I'm working on that displays SQL query results in a DataGrid. So far I am getting a blank grid. My code is as follows:

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

And this is the code for the grid

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;
        }
    }
}
6
  • Except that you have a DataGrid inside a DataGrid, there's really nothing wrong with your code, as long as your query is returning data. Commented Feb 9, 2017 at 23:23
  • Could you tell me where I'm duplicating? Commented Feb 9, 2017 at 23:36
  • In your XAML...... Commented Feb 9, 2017 at 23:37
  • Take a look at my sample. Obviously i used a different data table. Commented Feb 9, 2017 at 23:41
  • Thanks, I know it was something stupid like that. Commented Feb 9, 2017 at 23:42

1 Answer 1

1

Your XAML should look like this:

<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>

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.