Click here to Skip to main content
15,894,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm adding some values in a DataGrid table, I can add but the values are displaying only when i click the header of DataGrid table.
I'm following MVVM, this problem arise only when i'm writing code in seperate ViewModal file instead code behind.

someone please give solution for this problem...

I have shared my code below.



C#
 <Label Content="Add Load Generator" Grid.Row="0"  Grid.Column="1"  FontFamily="Ebrima" FontSize="15" HorizontalAlignment="left" VerticalAlignment="bottom" ></Label>
                <TextBox Text="{Binding Path=TxtIpAddress}"  Grid.Row="0" Grid.Column="2" Width="220"  Height="25" HorizontalAlignment="left" VerticalAlignment="bottom"  />
<pre lang="xml"><Button Content="Add" Command="{Binding AddIp}"  Grid.Row="0" Grid.Column="1" FontFamily="Ebrima" FontSize="12" Width="60" Height="25"  HorizontalAlignment="right" VerticalAlignment="center"/>




<DataGrid ItemsSource="{Binding SystemInformation}" AutoGenerateColumns="false"     Grid.Row="0" Grid.Column="1" CanUserAddRows="False">
   <DataGrid.Columns>
   <DataGridTextColumn Binding="{Binding Sno}"  Header="S.No" MinWidth="50" />
   <DataGridTextColumn Binding="{Binding strIpAddr}" Header="System Name" MinWidth="240"/>
   <DataGridTextColumn Binding="{Binding strSystemName}" Header="IP Address" MinWidth="240" />
   <DataGridTextColumn Binding="{Binding strStatus}" Header="Status" MinWidth="140" />
   </DataGrid.Columns>
</DataGrid>
</pre>



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.Mvvm;

using LoadTest_LibraryFiles.Views;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using System.Collections.ObjectModel;
using Microsoft.Win32;

//using Microsoft.Win32.OpenFileDialog;
using System.IO;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using System.Net;

namespace LoadTest_LibraryFiles.ViewModal
{

    public class UserBase_ViewModal : BindableBase
    {

 private int num = 1;
        ObservableCollection<HostSystemInformation> _HostSystemInformation = new ObservableCollection<HostSystemInformation>();
         public UserBase_ViewModal()
        {
           
            SystemInformation = new List<HostSystemInformation>();
            addIp = new DelegateCommand(AddSystemInformationInIpTable);
        }

 private String txtIpAddress;
        public String TxtIpAddress
        {
            get { return txtIpAddress; }
            set { SetProperty(ref txtIpAddress, value); }
        }


        private DelegateCommand addIp;
        public ICommand AddIp
        {
            get { return addIp; }
        }

        private List<HostSystemInformation> systemInformation;
        public List<HostSystemInformation> SystemInformation
        {
            get { return systemInformation; }
            set { SetProperty(ref systemInformation, value); }
        }



   private void AddSystemInformationInIpTable()
        {
            try
            {
              
                #region Add IP Address in IP Table

                systemInformation.Add(new HostSystemInformation
                {
                    Sno = num++,
                    strIpAddr = txtIpAddress,
                    strSystemName = hostname,
                    strStatus = "connected"

                });
                OnPropertyChanged(() => SystemInformation);
                
                #endregion


            }
            catch (System.ArgumentException) { MessageBox.Show("Enter the IP Address"); }
            catch (System.Net.Sockets.SocketException) { MessageBox.Show("Host not connected / No such host"); }
        }
}

 public class HostSystemInformation
    {
        public int Sno { get; set; }
        public string strIpAddr { get; set; }
        public string strSystemName { get; set; }
        public string strStatus { get; set; }
    }
         
}




Thanks in advance....
R.Karthik
Posted
Updated 8-Nov-14 0:19am
v4

1 solution

You should use ObservableCollection instead of List. Declare SystemInformation as:
C#
public ObservableCollection<HostSystemInformation> SystemInformation { get; private set; }


Then initialize it in constructor:
C#
SystemInformation = new ObservableCollection<HostSystemInformation>();


Finally in AddSystemInformationInIpTable:
C#
SystemInformation.Add(new HostSystemInformation
{
    Sno = num++,
    strIpAddr = txtIpAddress,
    strSystemName = hostname,
    strStatus = "connected"
 
});
// no need to call property changed explictly
// OnPropertyChanged(() => SystemInformation);


You have already declared _HostSystemInformation as ObservableCollection but it's not used anywhere. This is how you should expose collections that change.
 
Share this answer
 
Comments
Karthik Ravi 9-Nov-14 10:57am    
Thank You Very much for your valuable solution its work very fine...and thanks again for pin point my mistakes...

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