I am trying to bind a datagrid from a Database table using Observablecollection. when I came across a similar post- link on this forum , I tried to implement it on my project. but still, I am struggling to have a proper result.
here is my code , kindly help me to find where I have gone wrong ?
XAML :
<Window x:Class="MVVM_DemoAppl.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModel="clr-namespace:MVVM_DemoAppl.ViewModels" Title="MainWindow" Height="350" Width="525">
<Window.DataContext><ViewModel:MainViewModel/></Window.DataContext><Grid><DataGrid ItemsSource="{Binding SystemStatusData}" AutoGenerateColumns="False" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" ><DataGrid.Columns><DataGridTextColumn Header="System" Width="auto" Binding="{Binding System}" /><DataGridTextColumn Header="Date" Width="auto" Binding="{Binding Date}"/><DataGridTextColumn Header="Type" Width="auto" Binding="{Binding Types}"/><DataGridTextColumn Header="Message" Width="auto" Binding="{Binding Messages}"/><DataGridTextColumn Header="Critical" Width="auto" Binding="{Binding Critical}"/></DataGrid.Columns></DataGrid></Grid></Window>
My Model class SystemStatus.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace MVVM_DemoAppl.Models
{
public class SystemStatus: INotifyPropertyChanged
{
public SystemStatus()
{
}
private string _system;
private DateTime _date;
private string _type;
private string _message;
private bool _critical;
public string Systems
{
get { return _system; }
set { _system= value; OnPropertyChanged("_system"); }
}
public DateTime Date
{
get { return _date; }
set { _date = value; OnPropertyChanged("Date"); }
}
public string Types
{
get { return _type; }
set { _type = value; OnPropertyChanged("Types"); }
}
public string Messages
{
get { return _message; }
set { _message = value; OnPropertyChanged("Messages"); }
}
public bool Critical
{
get { return _critical; }
set { _critical = value; OnPropertyChanged("Critical"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
}My ViewModel class
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Data.EntityModel;
using MVVM_DemoAppl.Models;
using MVVM_DemoAppl;
namespace MVVM_DemoAppl.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
{
Model _myModel = new Model();
private ObservableCollection<SystemStatus> _systemStatusData= new ObservableCollection<SystemStatus>();
public ObservableCollection<SystemStatus> SystemStatusData
{
get { return _systemStatusData; }
set
{
_systemStatusData= value;
OnPropertyChanged("SystemStatusData");
}
}
public MainViewModel()
{
initializeload();
}
private void initializeload()
{
DataTable table = _myModel.getData();
for (int i = 0; i < table.Rows.Count; ++i)
SystemStatusData.Add(new SystemStatus
{
Systems= table.Rows[i][0].ToString(),
Date =Convert.ToDateTime(table.Rows[i][1]),
Types = table.Rows[i][2].ToString(),
Messages = table.Rows[i][3].ToString() Critical = Convert.ToBoolean(table.Rows[i][1]),
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyname)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyname));
}
}
public class Model
{
string con = ConfigurationManager.AppSettings["ConnectionStrings"];
public DataTable getData()
{
DataTable ndt = new DataTable();
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
SqlDataAdapter da = new SqlDataAdapter("select top 5 System,Date,Typ,Message,Critical from test_DB.dbo.SystemStatus",con);
da.Fill(ndt);
return ndt;
}
}
}My MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MVVM_DemoAppl.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}App.xaml.cs :
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using MVVM_DemoAppl.ViewModels;
using MVVM_DemoAppl.Views;
namespace MVVM_DemoAppl
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var mainWindow = new MainWindow();
var viewModel = new MainViewModel();
mainWindow.Show();
}
}
}I have error in the following content of my MainWindow.xaml as
"Cannot create an instance of "MainViewModel".
H:\MVVM_DemoAppl\MVVM_DemoAppl\MainWindow.xaml"
I tired replacing Datacontext in Xaml by ItemsSource for the Datagrid, but still I am having this error being raised.
kindly help me to overcome this problem. Thanks indeed.