この問題についていくつか検索を行ったところ、WPF で DataGrid を使用する主な方法がいくつかあることがわかりました。私はまだWPFと「バインディング」データ全般を学んでいるので、ヒントをいただければ幸いです。
ここに示されているコードは機能しますが、自分のデータをあまり制御できていないように感じます。Windows フォームとデータグリッドに関する私の経験のほとんどは、GridView に関するものです。
編集:私の質問が実際に何であるかについて、いくつかの混乱があります。明確にさせてください: コメントアウトされたフォーム コードは、カスタム列を作成します。これらのカスタム列に ObservableCollection のデータをプログラムで入力するにはどうすればよいですか。私がここでするように:
foreach (Customer cmr in customerList)
{
customerGridView.Rows.Add(cmr.customerID, cmr.customerName, cmr.customerArea);
}
私が知らない DataGrid を操作する簡単な方法はありますか?
これが私がいる場所です:
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Customer customer = new Customer();
ObservableCollection<Customer> customerList = new ObservableCollection<Customer>();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
//DataGridTextColumn c1 = new DataGridTextColumn();
//c1.Header = "Customer ID";
//c1.Binding = new Binding("CustomerID");
//customerDg.Columns.Add(c1);
//DataGridTextColumn c2 = new DataGridTextColumn();
//c2.Header = "Customer Name";
//c2.Binding = new Binding("CustomerName");
//customerDg.Columns.Add(c2);
//DataGridTextColumn c3 = new DataGridTextColumn();
//c3.Header = "Customer Area";
//c3.Binding = new Binding("CustomerArea");
//customerDg.Columns.Add(c3);
customerList = customer.GetAllCustomers();
customerDg.ItemsSource = customerList;
//foreach (Customer cust in customerList)
//{
// customerDg.Items.Add(cust);
//}
}
}
}
namespace WpfApplication1.Data_Layer
{
class Customers
{
static ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["MSAccess1"];
// Return a list of all products
public static ObservableCollection<Customer> GetAllCustomers()
{
Customer customer;
ObservableCollection<Customer> AllCustomers = new ObservableCollection<Customer>();
string p = "SELECT * FROM customer;";
using (OleDbConnection dbConn = new OleDbConnection(settings.ConnectionString))
{
try
{
dbConn.Open();
using (OleDbCommand cmd = dbConn.CreateCommand())
{
cmd.CommandText = p;
using (OleDbDataReader dbReader = cmd.ExecuteReader())
{
// make sure we have 1 or more rows in the data set
while (dbReader.Read())
{
customer = new Customer();
customer.customerID = Convert.ToInt32(dbReader["CustomerID"]);
customer.customerName = dbReader["CustomerName"].ToString();
customer.customerArea = Convert.ToChar(dbReader["CustomerArea"].ToString());
AllCustomers.Add(customer);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong: " + ex);
}
finally
{
dbConn.Close();
}
}
return AllCustomers;
}
}
}
namespace WpfApplication1.Business_Layer
{
class Customer
{
public int customerID { get; set; }
public string customerName { get; set; }
public char customerArea { get; set; }
public Customer()
{
customerID = 0;
customerName = "";
customerArea = 'X';
}
public Customer(int customerID, string customerName, char customerArea)
{
this.customerID = customerID;
this.customerName = customerName;
this.customerArea = customerArea;
}
public ObservableCollection<Customer> GetAllCustomers()
{
return Customers.GetAllCustomers();
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="305.263" Loaded="Window_Loaded_1">
<Grid Margin="0,0,-8,0">
<TextBox x:Name="customerNumberTxt" HorizontalAlignment="Left" Height="23" Margin="163,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="customerNameTxt" HorizontalAlignment="Left" Height="23" Margin="163,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="customerAreaTxt" HorizontalAlignment="Left" Height="23" Margin="163,66,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label x:Name="customerNumberLbl" Content="Cutomer number:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.242,-2.146" Width="130"/>
<Label x:Name="customerNameLbl" Content="Cutomer name:" HorizontalAlignment="Left" Margin="10,35,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.242,-2.146" Width="130"/>
<Label x:Name="customerAreaLbl" Content="Cutomer area:" HorizontalAlignment="Left" Margin="10,63,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.242,-2.146" Width="130"/>
<DataGrid x:Name="customerDg" AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="10,105,0,0" VerticalAlignment="Top" Height="205" Width="273">
<!--<DataGrid.Columns>
<DataGridTextColumn Header="Some header" ></DataGridTextColumn>
<DataGridTextColumn Header="Some header 2"></DataGridTextColumn>
<DataGridTextColumn Header="Some header 3"></DataGridTextColumn>
</DataGrid.Columns>-->
</DataGrid>
</Grid>
</Window>