question

JimJupiter-5178 avatar image
0 Votes"
JimJupiter-5178 asked ZhanglongWu-MSFT commented

How can I bind a datatable to a datagrid without autogenerateColumns

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>




windows-wpf
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Make sure DatenTB is IEnumerable, ie List<Data> or ObservableCollection<Data>. You don't need DataTable in this case.

0 Votes 0 ·

yeah but i would like to use it - it works before - maybe another error?

0 Votes 0 ·
EmonHaque-1485 avatar image
1 Vote"
EmonHaque-1485 answered EmonHaque-1485 edited

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; }
 }

capture.png (30.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JimJupiter-5178 avatar image
1 Vote"
JimJupiter-5178 answered ZhanglongWu-MSFT commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JimJupiter-5178,

Thanks for sharoing your solution.

Best regards,
Zhanglong

0 Votes 0 ·