All I'm trying to do is populate a DataGrid with items from a local SQL Server database called Inventory. I'm not getting any errors, but my table is blank.
FYI: the Inventory database has a table called Item which has columns ItemNumber, Name, Cost, and QuantityOnHand. The DataGrid name is called dgInventory. Any help would be appreciated.
Here's my .xaml code:
<UserControl x:Class="FinalAssignment.Views.InventoryView"
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:FinalAssignment.Views"
mc:Ignorable="d">
<Grid>
<DataGrid Name="dgInventory" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Item Number" Binding="{Binding ItemNumber}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Cost" Binding="{Binding Cost}" />
<DataGridTextColumn Header="Quantity on Hand" Binding="{Binding QuantityOnHand}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Details}" Margin="10" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</UserControl>
Here's my .cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using System.Data;
using System.Data.SqlClient;
namespace FinalAssignment.Views
{
/// <summary>
/// Interaction logic for InventoryView.xaml
/// </summary>
public partial class InventoryView : UserControl
{
public InventoryView()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strConnection = "Server=ASHLEY-PC\\SQLEXPRESS;Database=Inventory;Trusted_Connection=true";
SqlConnection con = new SqlConnection(strConnection);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "select ItemNumber, Name, Cost, QuantityOnHand from Item";
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dt = ((DataView)dgInventory.ItemsSource).ToTable();
sqlDataAdap.Fill(dt);
}
}
}