私がしようとしているのは、ローカルSQL Serverデータベースの項目であるInventory
をDataGridに取り込むことだけです。私は何のエラーもないが、私のテーブルは空白だ。SQLGridデータベースを使用してDataGridを実装する
はFYI:Inventory
データベースは、列ItemNumber
、Name
、Cost
、及びQuantityOnHand
を有するItem
と呼ばれるテーブルを有しています。 DataGridの名前はdgInventory
です。どんな助けもありがとう。
ここに私の.xamlコードです:
<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>
ここに私は.csコードだ:
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);
}
}
}
ありがとうございました! gridEmployeesの代わりに、dgInventoryを意味しますか? –
はい、投稿を更新しました。見てください..あなたの問題を解決することを願って –
うーん。まだ動かない。私はそれが私のstrConnectionと関係があると推測しています。私のサーバー名はASHLEY-PC \ SQLEXPRESSです。 : –