Filter like this:
if(MyProperty == null)
{
MyProperty = string.Empty;
]
Question
I'm new programming with C# in WPF, and I have an error:
I've bind my WPF DataGRid with an Observable Collection that receive parameters from a DataBase. First, when the Observable Collection receive all the parameters from the Stored Procedure the APP function normally, but when I have a null parameter into my data source (StoredProcedure), so the Observable Collection receive a null parameter, the application crash, and it don´t start.... the page just remain in blank
How to fix that problem ?
First Here I show the class I used as a Dada Context to my Page
class ColeccionDeDatos : INotifyPropertyChanged
{
private RegistroBLL regBll = new RegistroBLL();
private DivisionBLL divBll = new DivisionBLL();
private BrigadaBLL briBll = new BrigadaBLL();
private BatallonBLL batBll = new BatallonBLL();
private TropasBLL tropBll = new TropasBLL();
private CompañiaBLL compBLL = new CompañiaBLL();
private EstudioBLL estBll = new EstudioBLL();
private RegistroFullBLL regFullBll = new RegistroFullBLL();
private ObservableCollection<RegistroFullBO> coleccionFullRegistro = new ObservableCollection<RegistroFullBO>();
public ObservableCollection<RegistroFullBO> ColeccionFullRegistro
{
get { return coleccionFullRegistro; }
set { coleccionFullRegistro = value; }
}
private ObservableCollection<RegistroBO> coleccionRegistro = new ObservableCollection<RegistroBO>();
public ObservableCollection<RegistroBO> ColeccionRegistro
{
get { return coleccionRegistro; }
set { coleccionRegistro = value; }
}
private ObservableCollection<DivisionBO> coleccionDivision = new ObservableCollection<DivisionBO>();
public ObservableCollection<DivisionBO> ColeccionDivision
{
get { return coleccionDivision; }
set { coleccionDivision = value; }
}
private ObservableCollection<BrigadaBO> coleccionBrigada = new ObservableCollection<BrigadaBO>();
public ObservableCollection<BrigadaBO> ColeccionBrigada
{
get { return coleccionBrigada; }
set { coleccionBrigada = value; }
}
private ObservableCollection<BatallonBO> coleccionBatallon = new ObservableCollection<BatallonBO>();
public ObservableCollection<BatallonBO> ColeccionBatallon
{
get { return coleccionBatallon; }
set { coleccionBatallon = value; }
}
private ObservableCollection<TropasBO> coleccionTropas = new ObservableCollection<TropasBO>();
public ObservableCollection<TropasBO> ColeccionTropas
{
get { return coleccionTropas; }
set { coleccionTropas = value; }
}
private ObservableCollection<CompañiaBO> coleccionCompañia = new ObservableCollection<CompañiaBO>();
public ObservableCollection<CompañiaBO> ColeccionCompañia
{
get { return coleccionCompañia; }
set { coleccionCompañia = value; }
}
private ObservableCollection<EstudioBO> coleccionEstudio = new ObservableCollection<EstudioBO>();
public ObservableCollection<EstudioBO> ColeccionEstudio
{
get { return coleccionEstudio; }
set { coleccionEstudio = value; }
}
public ColeccionDeDatos()
{
ColeccionRegistro = regBll.ObtenerFilasRegistro();
ColeccionDivision = divBll.ObtenerFilasDivision();
ColeccionBrigada = briBll.ObtenerFilasBrigada();
ColeccionBatallon = batBll.ObtenerFilasBatallon();
ColeccionTropas = tropBll.ObtenerFilasTropas();
ColeccionCompañia = compBLL.ObtenerFilasCompañia();
ColeccionEstudio = estBll.ObtenerFilasEstudio();
ColeccionFullRegistro = regFullBll.ObtenerFilasRegistro();
}
Then In the code behind of the page a asign an instance of this class as DataContext for the Page element.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
try
{
PagRegistroName.DataContext = colData;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "+++++++++++");
}
}
And for my Data Grid this one:
<DataGrid Name="dgRegistro" Margin="5" SelectionChanged="dgRegistro_SelectionChanged"
ItemsSource="{Binding Path=ColeccionFullRegistro}" AutoGenerateColumns="False">
In each cell template I used a combobox:
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate>
<ComboBox x:Name="cmbDivision" Text="{Binding Path=Division, Mode=TwoWay}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Page, AncestorLevel=1},
Path=DataContext.ColeccionDivision}" DisplayMemberPath="Nom_division" SelectionChanged="cmbDivision_SelectionChanged" SelectedValuePath="Nom_division">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
so it is function a well as I want
which corresponds to the table in DataBase
BUT, when I put a NULL value directly in the DataBase
The App start but still in blank everytime, so something is wrong..
Any idea what is the problem ?
Solution
Filter like this:
if(MyProperty == null)
{
MyProperty = string.Empty;
]