我已经对这个问题进行了一些搜索,我发现有几种主要的方法可以在WPF中使用DataGrid。我仍在学习WPF和“绑定”数据,所以任何提示都会受到赞赏。
这里所示的代码功能齐全,但我觉得我对我的数据没有太多的控制权。我对Windows窗体和数据网格的大多数使用都是使用GridView。
编辑:我的问题实际上存在一些混淆。让我澄清一下:注释掉的表单代码将创建自定义列。如何使用来自ObservableCollection的数据以编程方式填充这些自定义coulmns。就像我在这里:
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>
答案 0 :(得分:4)
不确定,但这可能对您有所帮助(这会添加类似CheckBox的列):
使用 DataGridTemplateColumn ,您可以制作自定义列。
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>