Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGrid Column XAML

I am using a DataGrid to show items from a database table, and I am using EF CodeFirst so the database query automatically generates an object.

Here is my XAML:

<DataGrid Name="details" Margin="0,20,0,0" ItemsSource="{Binding}">
</DataGrid>

And this is the code behind it:

data = new DbLayer();
        int cardNumId = (from dataCardNum in data.creditCards
                         where dataCardNum.creditCardNumber == cardNum
                         select dataCardNum.Id).First();
        debits =new ObservableCollection<Debit>(( from billings in data.charges
                   where billings.creditCardNumber.Id == cardNumId
                   select billings).ToList());
        DataContext = debits;

That resolves in filling my DataGrid with all the information from my database. The only problem is that I have two columns that I don't want to show. I tried to create a dataTemplate that will generate the grid with the columns I want, but when I bind it to the datacontext it showed no information.

Here is my dataTemplate:

<DataTemplate x:Key="debitShow" DataType="DataTemplate:MonthBill.Debit">
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Header="amount" Binding="{Binding amount}"/>
                <DataGridTextColumn Header="charge date" Binding="{Binding chargeDate}"/>
                <DataGridCheckBoxColumn Header="charged" Binding="{Binding charged}"/>
                <DataGridTextColumn Header="store name" Binding="{Binding storeName}"/>
                <DataGridTextColumn Header="purchase date" Binding="{Binding debitDate}"/>
                <DataGridTextColumn Header="description" Binding="{Binding description}"/>
            </DataGrid.Columns>
        </DataGrid>
    </DataTemplate>

window xaml:

Debit class(the key attribute is for the codefirst database creation):

class Debit
{        
    [Key]
    public int Id { get; set; }
    public int amount { get; set; }
    public string storeName { get; set; }
    public DateTime debitDate { get; set; }
    public DateTime chargeDate { get; set; }
    public string description { get; set; }
    public creditCard creditCardNumber { get; set; }
    public bool charged { get; set; }
}

Any ideas?

like image 340
alostr Avatar asked Sep 01 '12 19:09

alostr


1 Answers

If your objective is to display your data without the two columns that you don't need, I would suggest taking the simpler approach of just specifying the columns of your grid explicitly:

<DataGrid  ItemsSource="{Binding}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="amount" Binding="{Binding amount}"/>
            <DataGridTextColumn Header="charge date" Binding="{Binding chargeDate}"/>
            <DataGridCheckBoxColumn Header="charged" Binding="{Binding charged}"/>
            <DataGridTextColumn Header="store name" Binding="{Binding storeName}"/>
            <DataGridTextColumn Header="purchase date" Binding="{Binding debitDate}"/>
            <DataGridTextColumn Header="description" Binding="{Binding description}"/>
        </DataGrid.Columns>
    </DataGrid>

Notice the AutoGenerateColumns="False" attribute.

I would only use a data template if I wanted to control the way the cells are rendered. If you are happy with the default presentation I think you don't need a template.

like image 137
Jakub Kaleta Avatar answered Sep 19 '22 21:09

Jakub Kaleta