1

我对这个问题进行了一些搜索,发现有几种主要方法可以在 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>
4

1 回答 1

8

不确定,但这可能会对您有所帮助(这会添加类似 CheckBox 的列):

使用DataGridTemplateColumn您可以制作自定义列。

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
于 2013-04-15T15:26:02.720 回答