1

I have done some searching on the issue and I have found there are a couple main ways to use a DataGrid in a WPF. I am still learning WPF and "binding" data in general so any tips are appreciated.

The code pictured here is functional, but I feel like I do not have very much control over my data. Most of my experience with windows forms and datagrids are with GridView.

EDIT: There is some confusion on what my question actually is. Let me clarify: The commented out form code will create custom columns. How do I programatically populate those custom coulmns with data from my ObservableCollection. Like I would here:

        foreach (Customer cmr in customerList)
        {
            customerGridView.Rows.Add(cmr.customerID, cmr.customerName, cmr.customerArea);
        }

Is there an easier way to work with a DataGrid that I dont know about?

Here is where I am:

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>
3
  • 3
    So what is the question? Commented Apr 15, 2013 at 15:18
  • I assume the question is in the title, but I don't know what the actual problem is. The code that has been commented out in Window_Loaded_1 should work. So where is the actual issue? Commented Apr 15, 2013 at 15:25
  • @anothershrubery uncommenting out that code yields new column headers that I can not populate with data. How do I populate those columns with data from my observable collection? Image: i.imgur.com/SrIFOJX.png Commented Apr 15, 2013 at 16:02

1 Answer 1

10

Not sure but this may helps you (this adds CheckBox-like columns):

Using DataGridTemplateColumn you can make custom columns.

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>
Sign up to request clarification or add additional context in comments.

2 Comments

That definitely gave me more row control. Can you send me a source for some reading on how all that XAML and templates work? Thats very interesting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.