Click here to Skip to main content
15,916,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
These are my tasks
1. When the MainWindow loads, the PatientsDataGrid should be populated from my database.
2. The textboxes from the Grid (x:Name="DetailsGrid") should be blank, except the IdTextBox.Text should be zero.
3. If I type a number in IdTextBox.Text and press SearchButton, the Other TextBoxes(LastName, FirstName, MiddleName) should be loaded with the corresponding record from the database. At the same time, the PatientsDataGrid must also filter its content into the Id that I searched from IdTextBox.
4. If I type some entries in the Last/First/Middle-NameTextBoxes and press the AddButton, it will be added in the database, as well as the PatientsDataGrid will be refreshed.
5. If I press the EditButton, the entry from the database will be updated the the PatientsDataGrid be refreshed.


But as I blindly try different binding, the result has gone to worse, the datagrid is not loaded with records, and the Search/Add/Update-Buttons are not functioning properly. Where did I go wrong?



Here is my mark up:

HTML
<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="500"
        WindowStartupLocation="CenterScreen"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    
    <Grid x:Name="DetailsGrid" Margin="10" >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock Text="Id" Grid.Row="0" Grid.Column="0"/>
            <TextBlock Text="Last Name" Grid.Row="1" Grid.Column="0"/>
            <TextBlock Text="First Name" Grid.Row="2" Grid.Column="0"/>
            <TextBlock Text="Middle Name" Grid.Row="3" Grid.Column="0"/>

            <TextBox x:Name="IdTextBox" Text="{Binding Id, Mode=TwoWay}" 
                     Grid.Row="0" Grid.Column="1" />
            <TextBox x:Name="LastTextBox" Text="{Binding LastName, Mode=TwoWay}" 
                     Grid.Row="1" Grid.Column="1" />
            <TextBox x:Name="FirstTextBox" Text="{Binding FirstName, Mode=TwoWay}"
                     Grid.Row="2" Grid.Column="1" />
            <TextBox x:Name="MiddleTextBox" Text="{Binding MiddleName, Mode=TwoWay}" 
                     Grid.Row="3" Grid.Column="1" />

            <TextBlock Text="Combo Boxing" Grid.Row="4" Grid.Column="0"  />
            <ComboBox x:Name="TryComboBox" Grid.Row="4" Grid.Column="1" />

            <Button x:Name="SearchButton" Content="Search" Grid.Row="0" Grid.Column="2" 
                    Click="SearchButton_OnClick"/>

            <Button x:Name="AddButton" Content="Add" Grid.Row="5" Grid.Column="0" 
                    Click="AddButton_OnClick"/>
            <Button x:Name="UpdateButton" Content="Update" 
                    Grid.Row="{Binding ElementName=AddButton, Mode=OneWay, Path=(Grid.Row)}" 
                    Grid.Column="1" Click="UpdateButton_OnClick"/>
            <Button x:Name="DeleteButton" Content="Delete" 
                    Grid.Row="{Binding ElementName=AddButton, Mode=OneWay, Path=(Grid.Row)}" 
                    Grid.Column="2" Click="DeleteButton_OnClick"/>
        </Grid>
        
        <DataGrid Name="PatientDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" 
                  Grid.Row="1"
                  IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id" Binding="{Binding Id}" />
                <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
                <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" />
                <DataGridTextColumn Header="Middle Name" Binding="{Binding MiddleName}" />
                <DataGridCheckBoxColumn Header="Status" Binding="{Binding StatusId}" 
                                        Visibility="Collapsed"/>
            </DataGrid.Columns>
           
        </DataGrid>
    </Grid>
</Window>






Here is my MainWindow code behind:

C#
public partial class MainWindow : Window
    {
        PatientMgr patientMgrModel = new PatientMgr();
        public MainWindow()
        {
            InitializeComponent();
            

            patientMgrModel.Entity.Id = 0;
            Refresher();
        }


        public void Refresher()
        {
            DetailsGrid.DataContext = patientMgrModel.Entity;

            patientMgrModel.EntityList = patientMgrModel.RetrieveMany(patientMgrModel.Entity);

            DataContext = patientMgrModel.EntityList;
        }

        private void AddButton_OnClick(object sender, RoutedEventArgs e)
        {
            
            patientMgrModel.Insert(patientMgrModel.Entity);
            Refresher();
        }

        private void UpdateButton_OnClick(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(String.Format("Id:{0} \nLast Name: {1} \nFirst Name: {2} \nMiddleName: {3}",
            //    patientViewModel.Id,
            //    patientViewModel.LastName,
            //    patientViewModel.FirstName,
            //    patientViewModel.MiddleName));

            patientMgrModel.Update(patientMgrModel.Entity);
        }


        private void DeleteButton_OnClick(object sender, RoutedEventArgs e)
        {
            patientMgrModel.Delete(patientMgrModel.Entity);
        }

        private void SearchButton_OnClick(object sender, RoutedEventArgs e)
        {
            patientMgrModel.Entity = patientMgrModel.Retrieve(patientMgrModel.Entity);

            DataContext = patientMgrModel.Entity;
        }
    }


Here is my BusinessLogicLayer:

C#
public class PatientMgr : INotifyPropertyChanged
    {
        private readonly PatientDb _db;
        private Patient _entity;
        private List<Patient> _entityList;

        public Patient Entity
        {
            get { return _entity; }
            set
            {
                if (Equals(value, _entity)) return;
                _entity = value;
                OnPropertyChanged();
            }
        }

        public List<Patient> EntityList
        {
            get { return _entityList; }
            set
            {
                if (Equals(value, _entityList)) return;
                _entityList = value;
                OnPropertyChanged();
            }
        }

        public PatientMgr()
        {
            _db = new PatientDb();
            Entity = new Patient();
            EntityList = new List<Patient>();
        }

        public Patient Retrieve(Patient parameters)
        {
            return _db.Retrieve(parameters);
        }

        public List<Patient> RetrieveMany(Patient parameters)
        {
            return _db.RetrieveMany(parameters);
        }

        public bool Insert(Patient entity)
        {
            return _db.Insert(entity);
        }

        public bool Update(Patient entity)
        {
            return _db.Update(entity);
        }

        public bool Delete(Patient entity)
        {
            return _db.Delete(entity);
        }


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }



I also have DataAccessLayer which contains methods that are being called using the BusinessLogicLayer above. It returns just fine so I will not post it here. The problems are stated in my second paragraph. I hope you could help me, I tried studying binding, but I think I needed a pattern first before I can understand most of what stated in different tutorials.
Posted
Updated 7-May-15 8:18am
v2

1 solution

List will not update the UI as you have not implemented the InotifyCollectionChanged event.

Use observablecollection here for the your entity list.

And inside the model use the InotifyPropertyChanged interface.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900