Click here to Skip to main content
15,064,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
in this cod i got result data in ListPersons but not displayed in datagrid
please guide me whtas my problem
thanks alot

What I have tried:

in view:
<pre><Window x:Class="WpfMasksTest.Echreshavi.View.PersonInfoTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfMasksTest.Echreshavi.View"
         xmlns:swc="http://schemas.staware.com/winfx/2008/xaml/controls"
         xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
          xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
        
        mc:Ignorable="d"
        Title="PersonInfoTest" Height="500" Width="1000">


 
       <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80"/>
            <RowDefinition/>
            <RowDefinition/>

        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0"   >
            <TextBox  Width="150" Height="30" Margin="10" Text="{Binding Path=V_SEARCH , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
            <Button Width="90" Margin="5" Content="Search Person" Command="{Binding SearchPersonCommand}"/>

        </StackPanel>

        <StackPanel Grid.Row="1"  >
            <DataGrid AutoGenerateColumns="False"
				  Height="200"
				  Margin="4"
				  ItemsSource="{Binding ListPersons}" 
				  SelectedItem="{Binding Path=SelectedItem}"
				  Background="#FFE0C300"
				  CanUserReorderColumns="False"
				  AlternatingRowBackground="#E6FCFCB8"
				  CanUserAddRows="False"
				  CanUserDeleteRows="False"
				  CanUserResizeRows="False"
				  SelectionMode="Single"
				  IsReadOnly="True">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Persons"
									Binding="{Binding Path=CustomerName}"
									Width="*" />
                    <DataGridTextColumn Header="State"
									Binding="{Binding Path=State}" />
                </DataGrid.Columns>
            </DataGrid>

        </StackPanel>

        <StackPanel Grid.Row="2"  >
            <dxe:ListBoxEdit ItemsSource="{Binding SelectedItems, ElementName=grid}"
                     DisplayMember="id"/>

        </StackPanel>
</Grid>
</Window>


in view model:

C#
<pre>namespace ViewModels.Person
{

    
    public class InfoPersonVM: INotifyPropertyChanged
    {

        private static readonly ILog _log = SwLogManager.GetLogger(typeof(InfoPersonVM));


        public InfoPersonVM()
        {
            SearchPersonCommand = new RelayCommand(ExecuteMyMethod, CanExecuteMyMethod);
            ObjPersCurSfld180 = new PersCurSfld180Dto();

            SwUserInfo.Login("mh", "de");
            _log.Warn(" HEI HIER ");

        }

     

        private string _seachperson;
        private PersCurSfld180Dto ObjPersCurSfld180;

        public ICommand SearchPersonCommand { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;

        
      public  IList<persrec> ListPersons { get; set; } = new ObservableCollection<persrec>();
        public DelegateCommand DeleteSelectedRowsCommand { get; private set; }

        
        //private ObservableCollection<persrec> listperson;

        //public ObservableCollection<persrec> ListPersons
        //{
        //    get { return listperson; }
        //    set
        //    {
        //        listperson = value;
        //        this.NotifyPropertyChanged("ListPersons");
        //    }
        //}

        private void NotifyPropertyChanged(string ListPersons)
        {
            throw new NotImplementedException();
        }

        public string V_SEARCH
        {
            get { return _seachperson; }
            set { _seachperson = value; }
        }



        private bool CanExecuteMyMethod(object parameter)
        {
            if (string.IsNullOrEmpty(V_SEARCH))
            {
                return false;
            }
            else
            {
                if (V_SEARCH != "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }


        private void ExecuteMyMethod(object parameter)
        {
         

            try
            {


                if (true)
                {

                    GetPersonInfo(V_SEARCH);

                }
                else
                {
                    // log.Error;
                }
            }
            catch (Exception e)
            {
                // log.Error;


                string x = e.ToString();
            }
        }



        public IList<persrec> GetPersonInfo(string  serchitem )
        {
            var objDto = new PersCurSfld180Dto()
            {

                Search = serchitem,
            };

            new PersDao().PersCurSfld180(ref objDto);
            _log.Info($"error:{objDto.Errno} AdrgruppeId:{objDto.AdrgruppeId}");

             ListPersons = objDto.CurPersonList;



            //ListPersons = (ObservableCollection<persrec>)objDto.CurPersonList; 

           // ListPersons = objDto.CurPersonList;
            return ListPersons;
           
        }



    }


}
Posted
Updated 19-Aug-21 0:14am
v2

1 solution

Your GetPersonInfo method replaces the value of the ListPersons property with a new IList<persrec>. But you never raise the PropertyChanged event for that property, so the UI has no way of knowing that the collection has been replaced and it needs to update the grid.

The simplest solution is to raise the PropertyChanged event every time you change the property:
C#
private IList<persrec> _listPersons = new List<persrec>();

public IList<persrec> ListPersons
{
    get { return _listPersons; }
    set { SetValue(ref _listPersons, value); }
}

private void SetValue<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
    if (Equals(field, value)) return;
    
    field = value;
    NotifyPropertyChanged(propertyName);
}

private void NotifyPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
   

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