title: unable to bind the observablecollection to the datagrid in MVVM
body: <p>am completely new to MVVM. I was trying to bind the data from sqlserver to the Datagrid in WPF and perform Edit, update and delete operations on it. 
  now, am unable to bind the data from sqlserver to datagrid atleast using observable collection in MVVM....
  someone please help me out to resolve it and also kindly let me know how to implement the edit,update and delete operations for the same datagrid....am totally getting confused implementing the MVVM architecture..</p>
  
  <p>with the following code I could bind the data to "Empdata"(observablecollection) but the datagrid is not being displayed at all.</p>
  
  <p>the following is my xaml code :</p>
  
  <pre><code>&lt;DataGrid    ItemsSource="{Binding Path=Empdata}" x:Name="dtgrdemp"
               AutoGenerateColumns="False"
               SelectionMode="Single"
               SelectionUnit="FullRow"
               GridLinesVisibility="Horizontal"
               CanUserDeleteRows="True"
               CanUserAddRows="False"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Name" Width="SizeToCells" MinWidth="125" Binding="{Binding Path=Ename}"/&gt;
          &lt;DataGridTextColumn Header="Age" Width="SizeToCells" MinWidth="200" Binding="{Binding Path=Eage}"/&gt;
          &lt;DataGridTextColumn Header="Description" Width="SizeToCells" MinWidth="200" Binding="{Binding Path=Edescription}"/&gt;
      &lt;/DataGrid.Columns&gt;&lt;/Datagrid&gt;
  </code></pre>
  
  <p>the following is my code for "view" where i took a class as person</p>
  
  <pre><code>public class Person : INotifyPropertyChanged, IDataErrorInfo
  {
      private string names;
  
      public string Names
      {
          get { return names; }
          set
          {
              names = value;
              OnPropertyChanged("Names");
          }
      }
  
      private int age;
  
      public int Age
      {
          get { return age; }
          set
          {
              age = value;
              OnPropertyChanged("Age");
          }
      }
  
      private string description;
  
      public string Description
      {
          get { return description; }
          set
          {
              description = value;
              OnPropertyChanged("Description");
          }
      }
  
  
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string propertyname)
      {
          var handler = PropertyChanged;
          if (handler != null)
              handler(this, new PropertyChangedEventArgs(propertyname));
      }
  
      public string Error
      {
          get { return null; }
      }
  
      public string this[string columnName]
      {
          get
          {
              string error = null;
              switch (columnName)
              {
                  case "Names":
                      if (string.IsNullOrEmpty(names))
                      {
                          error = "First Name required";
                      }
                      break;
  
                  case "Age":
                      if ((age &lt; 18) || (age &gt; 85))
                      {
                          error = "Age out of range.";
                      }
                      break;
                  case "Description":
                      if (string.IsNullOrEmpty(description))
                      {
                          error = "Last Name required";
                      }
                      break;
              }
  
              return (error);
          }
      }
  </code></pre>
  
  <p>the following is my code for "ViewModel" class</p>
  
  <pre><code>public class MainViewModel :INotifyPropertyChanged
  {
      string con = ConfigurationSettings.AppSettings["ConnectionStrings"];
      ObservableCollection&lt;EmpInfo&gt; Empdata= new ObservableCollection&lt;EmpInfo&gt;();
  
      private Person empperson;
  
      public Person Empperson
      {
          get { return empperson; }
          set { empperson = value; }
      }
  
      public MainViewModel()
      {
          initializeload();
      }
  
      private void initializeload()
      {
          DataTable dt = new DataTable();
          Empdata = new ObservableCollection&lt;EmpInfo&gt;();
          Empdata.Add(new EmpInfo(dt));
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyname)
      {
          var handler = PropertyChanged;
          if (handler != null)
              handler(this, new PropertyChangedEventArgs(propertyname));
      }
  }
  </code></pre>
  
  <p>the following is the code for EmpInfo.cs class</p>
  
  <pre><code>public EmpInfo(DataTable dt)
  {
      SqlConnection sqlcon = new SqlConnection(con);
      sqlcon.Open();
      SqlDataAdapter da = new SqlDataAdapter("Select Ename,Eage,Edescription from EmployeeTable", sqlcon);
      da.Fill(dt);
      this.dt = dt;
  }
  </code></pre>
  
  <p>the following is the code in Appxaml.cs</p>
  
  <pre><code>protected override void OnStartup(StartupEventArgs e)
  {
      base.OnStartup(e);
      var mainWindow = new MainWindow();
      var viewModel = new MainViewModel();
      mainWindow.DataContext = viewModel;
      mainWindow.Show();
  }
  </code></pre>