How can I bind a datatable to a datagrid without autogenerateColumns

Jim Jupiter 66 Reputation points
2021-07-16T11:05:51.767+00:00

Hi

I have created a class with a datatable

public DataTable DatenTB = new DataTable();

    public void initDataTB()
    {

        DatenTB.Columns.Add("ID", typeof(string));
        DatenTB.Columns.Add("Name", typeof(string));

...
}

bind it to datagrid

InitializeComponent();


        DT.initDataTB();
        DT.loadDataTB();

        dGrid.DataContext = DT.DatenTB;

and it works ... with autogeneratedColums

Now I would like to use custom columns -
set autogenerated to false and it doesn't work - Column header appears but no data
didn't find the error - any help?

<Window.DataContext>
        <local:DatenTable/>
    </Window.DataContext>


 <DataGrid ItemsSource="{Binding DatenTB}" ...



 <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            </DataGrid.Columns>
Developer technologies | Windows Presentation Foundation
{count} votes

2 answers

Sort by: Most helpful
  1. Emon Haque 3,176 Reputation points
    2021-07-16T12:07:09.487+00:00

    You can have something like this in your viewmodel:

    class MainVM  
    {  
        public DataTable Entries { get; set; }  
        public MainVM() {  
            Entries = new DataTable() {  
                Columns = {  
                    new DataColumn("DrHead", typeof(string)),  
                    new DataColumn("CrHead", typeof(string)),  
                    new DataColumn("DrAmount", typeof(int)),  
                    new DataColumn("CrAmount", typeof(int)),  
                }  
            };  
            for (int i = 0; i < 5; i++)   
                Entries.Rows.Add("Dr", "Cr", 100, 200);  
        }  
    }  
    

    and these in xaml:

    <Window x:Class="WPFTest.MainWindow"...>  
        <Window.DataContext>  
            <local:MainVM/>  
        </Window.DataContext>  
        <Grid>  
            <DataGrid ItemsSource="{Binding Entries}"  
                    AutoGenerateColumns="False">  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="Dr Head" Binding="{Binding DrHead}"/>  
                    <DataGridTextColumn Header="Cr Head"  Binding="{Binding CrHead}"/>  
                    <DataGridTextColumn Header="Dr Amount" Binding="{Binding DrAmount}"/>  
                    <DataGridTextColumn Header="Cr Amount"  Binding="{Binding CrAmount}"/>  
                </DataGrid.Columns>  
            </DataGrid>  
        </Grid>  
    </Window>  
    

    and you'll get this:

    115428-capture.png

    No one uses this approach. Use List, ObservableCollection, etc. instead.

    EDIT
    ---
    With ObservableCollection/List, you simply do this:

    class MainVM  
    {  
        public ObservableCollection<Entry> Entries { get; set; }  
        public MainVM() {  
            Entries = new ObservableCollection<Entry>();  
            for (int i = 0; i < 5; i++) {  
                Entries.Add(new Entry() {  
                    DrHead = "Dr",  
                    CrHead = "Cr",  
                    DrAmount = 100,  
                    CrAmount = 200  
                });  
            }  
        }  
    }  
    class Entry  
    {  
        public string DrHead { get; set; }  
        public string CrHead { get; set; }  
        public int DrAmount { get; set; }  
        public int CrAmount { get; set; }  
    }  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Jim Jupiter 66 Reputation points
    2021-07-16T15:39:01.12+00:00

    Your first approach is the one I do - the second I tried before with celleditending and update observableCollection and so on

    for my little App is seems easier to use datatable like in the old times and ...

    I found the error ... I changed

    <DataGrid ItemsSource="{Binding DatenTB}" 
    

    to

    <DataGrid ItemsSource="{Binding}" 
    

    and it's working :)

    Thanks for all your answers

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.