Only3lue Only3lue - 8 months ago 55
C# Question

Datagrid with Databinding stays empty but collection has objects

I have made a databinding to DataGrid with a Collection-Class in WPF.
I made the following things for the Binding.


internal class ZeichnungCollection : ObservableCollection<ZeichnungInDB>
public ZeichnungCollection() : base()

The Object-Class for the Collection:

class ZeichnungInDB : ISerializable
{...//Properties and Constructors
public void GetObjectData(SerializationInfo info, StreamingContext context)
this.GetObjectData(info, context);
info.AddValue("Zeichnungsnummer", Zeichnungsnummer, typeof(string));
info.AddValue("Index", Index, typeof(string));
info.AddValue("Volante-Index", Volante_Index, typeof(int));
info.AddValue("Änderung Intern", Aenderung_Int, typeof(string));
info.AddValue("Änderung Extern", Aenderung_Ext, typeof(string));

The Code of the Window:

public Anzeigen()
zeichnungen = new ZeichnungCollection();
dataInbox.ItemsSource = zeichnungen;
private void btnSearch_Click(object sender, RoutedEventArgs e)
int volIndex = -1;
if (checkAktuell.IsChecked == true && !txtZeichnungsnummer.Equals(""))
volIndex = DBZugriff.DBZugriff.getVolCountByDrawingNumber(txtZeichnungsnummer.Text);
int tagID = DBZugriff.DBZugriff.getTagIdByTag(txtTags.Text, int.Parse(txtProjektnummer.Text));
int projektID = DBZugriff.DBZugriff.getProjectIdByProjectnumber(int.Parse(txtProjektnummer.Text));
status = cmbStatus.Text;
mmsSachmerkmal = cmbMMSSachmerkmal.Text;
dokTyp = cmbDokTyp.Text;
extension = cmbDatEnd.Text;
zeichnungen = DBZugriff.DBZugriff.findDrawings(projektID, txtZeichnungsnummer.Text, tagID, status, mmsSachmerkmal, dokTyp, extension, volIndex);

The Collection should be filled when clicking on the Searchbutton. But it shows me one empty row when I call the Page. So if the Collection is empty there should be no rows in the Datagrid, right? I don´t fill the Collection in a other place in the Window and I checked if the Collection is really empty. So I don´t get it why there is one empty row.

The xaml-Binding Code:

<DataGrid x:Name="dataInbox" Grid.Row="1" Width="654" MaxHeight="500" AutoGenerateColumns="False" ItemsSource="{Binding Source=ZeichnungCollection}" HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="0,5">
<DataGridTextColumn Header="Zeichnungsnummer" Binding="{Binding Zeichnungsnummer, UpdateSourceTrigger=LostFocus}" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Header="Index" Binding="{Binding Index, UpdateSourceTrigger=LostFocus}" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Header="Volante-Index" Binding="{Binding Volante-Index, UpdateSourceTrigger=LostFocus}" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Header="Änderung Intern" Binding="{Binding Änderung Intern, UpdateSourceTrigger=LostFocus}" Width="Auto" IsReadOnly="True"/>
<DataGridTextColumn Header="Änderung Extern" Binding="{Binding Änderung Extern, UpdateSourceTrigger=LostFocus}" Width="Auto" IsReadOnly="True"/>

The Select works. I checked that and the Collection is filled with the right objects but the DataGrid stays empty and I don´t know why.
Has someone an Idea what the problem is or did I something wrong with the binding?

I have a second DataGrid in this Project on a other Page with a other Collection but with the same Structure like this and there it works fine.

Answer Source

i think you create a new instance of zeichnungen in this line

zeichnungen = DBZugriff.DBZugriff.findDrawings(projektID, txtZeichnungsnummer.Text, tagID, status, mmsSachmerkmal, dokTyp, extension, volIndex);

so you better use foreach with add to fill the collection.

foreach(var item in  DBZugriff.DBZugriff.findDrawings(projektID, txtZeichnungsnummer.Text, tagID, status, mmsSachmerkmal, dokTyp, extension, volIndex))

or even better switch to MVVM with INotifyPropertyChanged and Bindings