【问题标题】:Gridview Change event not firing and how to refresh the grid after inserting a record?Gridview 更改事件未触发以及插入记录后如何刷新网格?
【发布时间】:2013-10-05 00:31:36
【问题描述】:

我试图通过 MVVM 模式获得 WPF 的技能。我几乎完成了具有基本功能的表格,但面临两个问题

1) 我的 Gridview 更改事件未触发 2) 插入记录后如何刷新网格

下面给出了我的 ViewModel 和 View 代码

查看模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using DatabaseLayer;
using System.Data;

namespace WPFnMVVM.ViewModel 
{
    public class ContactsViewModel : WPFnMVVM.Common.VMBase
    {
        #region Variables
        private int _Id;
        private string _First_Name;
        private string _Last_Name;
        private DateTime _DOB;
        private clstbl_Contacts _Contacts;
        public WPFnMVVM.Common.RelayCommand _addCommand;
        public DataTable _tblContacts;
        #endregion

        public ContactsViewModel()
        {

            _tblContacts = LoadContacts();
        }


        #region Public Properties
        public int Id
        {
            get { return _Id; }
            set { _Id = value; OnPropertyChanged("Id"); }
        }
        public string First_Name
        {
            get { return _First_Name; }
            set { _First_Name = value; OnPropertyChanged("First_Name"); }
        }
        public string Last_Name
        {
            get { return _Last_Name; }
            set { _Last_Name = value; OnPropertyChanged("Last_Name"); }
        }
        public DateTime DOB
        {
            get { return _DOB; }
            set { _DOB = value; OnPropertyChanged("DOB"); }
        }
        public clstbl_Contacts Contacts
        {
            get { return _Contacts; }
            set
            {
                _Contacts = value;
                OnPropertyChanged("Contacts");
            }


        }

        public DataTable ContactsList
        {
            get { return _tblContacts; }
            set
            {
                _tblContacts = value;
                OnPropertyChanged("ContactsList");
            }
        }
        #endregion

        #region Private Methods
        private DataTable LoadContacts()
        {
            clstbl_Contacts objContact = new clstbl_Contacts(WPFnMVVM.Common.clsSettings.ConStr);
            {
                return objContact.Select();


            };
        }

        private void AddContacts()
        {
            clstbl_Contacts objContacts = new clstbl_Contacts(WPFnMVVM.Common.clsSettings.ConStr);
            objContacts.First_Name = First_Name;
            objContacts.Last_Name = Last_Name;
            objContacts.DOB = DOB;
            objContacts.Insert();
        }

        #endregion

        #region Commands
          public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new WPFnMVVM.Common.RelayCommand(
                        param => this.AddContacts(),
                        param => true
                        );
                }
                return _addCommand;
            }
        }
        #endregion

    }
}

查看

<Window x:Class="WPFnMVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:VM="clr-namespace:WPFnMVVM.ViewModel"
        xmlns:View="clr-namespace:WPFnMVVM"
        Title="MainWindow" Height="350" Width="337">
    <Window.DataContext>
        <VM:ContactsViewModel/>
    </Window.DataContext>
    <Grid Name="MyGrid">
        <TextBox HorizontalAlignment="Left" Height="23" Margin="95,13,0,0" TextWrapping="Wrap" Text="{Binding Path=First_Name, UpdateSourceTrigger=PropertyChanged}"  VerticalAlignment="Top" Width="120" />
        <TextBox HorizontalAlignment="Left" Height="23" Margin="95,47,0,0" TextWrapping="Wrap" Text="{Binding Path=Last_Name, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <Label Content="First Name" HorizontalAlignment="Left" Margin="8,10,0,0" VerticalAlignment="Top"/>
        <Label Content="Last Name" HorizontalAlignment="Left" Margin="9,47,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.68,1.974"/>
        <Label Content="DOB" HorizontalAlignment="Left" Margin="8,75,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.68,1.974"/>
        <DatePicker Height="25" HorizontalAlignment="Left" Margin="95,76,0,0" Name="datePicker1"
            VerticalAlignment="Top" Width="120" SelectedDate="{Binding Path=DOB, UpdateSourceTrigger=PropertyChanged}"/>
        <Button Content="Add" HorizontalAlignment="Left" Margin="9,118,0,0" VerticalAlignment="Top" Width="75" Command="{Binding Path=AddCommand}"/>
        <Button Content="Update" HorizontalAlignment="Left" Margin="102,118,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Content="Delete" HorizontalAlignment="Left" Margin="193,118,0,0" VerticalAlignment="Top" Width="75"/>
        <ListView BorderBrush="White" ItemsSource="{Binding Path=ContactsList, UpdateSourceTrigger=PropertyChanged}"
                   HorizontalAlignment="Stretch" Margin="11,156,10,10" SelectedValue="{Binding Path=Contacts, UpdateSourceTrigger=PropertyChanged}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="First Name" 
                                    DisplayMemberBinding="{Binding Path=First_Name}" Width="70" />
                    <GridViewColumn Header="Last Name"
                                    DisplayMemberBinding="{Binding Path=Last_Name}" Width="70" />
                    <GridViewColumn Header="DOB" 
                                    DisplayMemberBinding="{Binding Path=DOB}" Width="70" />

                </GridView>
            </ListView.View>
        </ListView >

    </Grid>
</Window>

如果可能的话,请指导并给我参考任何具有 sql crud 功能的实际应用程序,我可以从中提高我的技能。

谢谢

【问题讨论】:

  • 首先,您的 Bindings 工作正常吗?
  • 用户 ObservableCollection 而不是 DataTable。
  • 嗨 Tico,是的,我的控件已绑定,结果也显示在列表视图中
  • 嗨 JasRaj Bishnoi,我想从数据库中分配数据,请告诉我如何使用 ObservableColletion 从数据库中获取数据并将其分配给 gridview

标签: c# wpf mvvm


【解决方案1】:

DataTable 没有实现 INotifyPropertyChanged,因此直接绑定到它会有问题。您将看到初始数据,但随后对数据的更改不会反映在视图中。

我想说最简单的替代方法是使用 DataView,它确实 实现了 INotifyPropertyChanged。您可以使用_tblContacts.DefaultView 轻松获取您的表的视图。

【讨论】:

    【解决方案2】:

    请查看下面的代码。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;
    using DatabaseLayer;
    using System.Data;
    
    namespace WPFnMVVM.ViewModel 
    {
        public class ContactsViewModel : WPFnMVVM.Common.VMBase
        {
            #region Variables
            private int _Id;
            private string _First_Name;
            private string _Last_Name;
            private DateTime _DOB;
            private clstbl_Contacts _Contacts;
            public WPFnMVVM.Common.RelayCommand _addCommand;
            public ObservableCollection<clstbl_Contacts> _ContactsList;
            #endregion
    
            #region Contructor
            public ContactsViewModel()
            {
                LoadContacts();
            }      
            #endregion
    
            #region Public Properties
            public int Id
            {
                get { return _Id; }
                set { _Id = value; OnPropertyChanged("Id"); }
            }
            public string First_Name
            {
                get { return _First_Name; }
                set { _First_Name = value;
    
                    OnPropertyChanged("First_Name"); }
            }
            public string Last_Name
            {
                get { return _Last_Name; }
                set { _Last_Name = value; OnPropertyChanged("Last_Name"); }
            }
            public DateTime DOB
            {
                get { return _DOB; }
                set { _DOB = value; OnPropertyChanged("DOB"); }
            }
            public clstbl_Contacts Contacts
            {
                get { return _Contacts; }
                set
                {
                    _Contacts = value;
    
                    OnPropertyChanged("Contacts");
                    GetValuesFromModel();
                }
    
    
            }
            public ObservableCollection<clstbl_Contacts> ContactsList
            {
                get { return _ContactsList; }
                set
                {
                    _ContactsList = value;
                    OnPropertyChanged("ContactsList");
                }
            }     
            #endregion
    
            #region Methods
            private void LoadContacts()
            {
    
                clstbl_Contacts objContact = new clstbl_Contacts(WPFnMVVM.Common.clsSettings.ConStr);
                DataTable dt = objContact.Select();
                _ContactsList = new ObservableCollection<clstbl_Contacts>();
    
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    _ContactsList.Add(new clstbl_Contacts { Id = Convert.ToInt16(dt.Rows[i]["ID"].ToString())
                                                            ,First_Name = dt.Rows[i]["First_Name"].ToString(),
                                                            Last_Name = dt.Rows[i]["Last_Name"].ToString(),
                                                            DOB = Convert.ToDateTime(dt.Rows[i]["DOB"].ToString())
                    });
                }
    
            }
            private void AddContacts()
            {
                clstbl_Contacts objContacts = new clstbl_Contacts(WPFnMVVM.Common.clsSettings.ConStr);
                objContacts.First_Name = First_Name;
                objContacts.Last_Name = Last_Name;
                objContacts.DOB = DOB;
                objContacts.Insert();
    
            }
    
            private void GetValuesFromModel()
            {
                Id = _Contacts.Id;
                First_Name = _Contacts.First_Name;
                Last_Name = _Contacts.Last_Name;
                DOB = _Contacts.DOB;
            }
            #endregion
    
            #region Commands
              public ICommand AddCommand
            {
                get
                {
                    if (_addCommand == null)
                    {
                        _addCommand = new WPFnMVVM.Common.RelayCommand(
                            param => this.AddContacts(),
                            param => true
                            );
                    }
                    return _addCommand;
                }
            }
            #endregion
    
        }
    }
    

    查看

    <Window x:Class="WPFnMVVM.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:VM="clr-namespace:WPFnMVVM.ViewModel"
            xmlns:View="clr-namespace:WPFnMVVM"
    
            Title="MainWindow" Height="350" Width="337">
        <Window.DataContext>
            <VM:ContactsViewModel/>
        </Window.DataContext>
        <Grid Name="MyGrid">
            <TextBox HorizontalAlignment="Left" Height="23" Margin="95,13,0,0" TextWrapping="Wrap" 
                     Text="{Binding Path=First_Name, UpdateSourceTrigger=PropertyChanged}"   VerticalAlignment="Top" Width="120" />
    
            <TextBox HorizontalAlignment="Left" Height="23" Margin="95,47,0,0" TextWrapping="Wrap" Text="{Binding Path=Last_Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
            <Label Content="First Name" HorizontalAlignment="Left" Margin="8,10,0,0" VerticalAlignment="Top"/>
            <Label Content="Last Name" HorizontalAlignment="Left" Margin="9,47,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.68,1.974"/>
            <Label Content="DOB" HorizontalAlignment="Left" Margin="8,75,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.68,1.974"/>
            <DatePicker Height="25" HorizontalAlignment="Left" Margin="95,76,0,0" Name="datePicker1"
                VerticalAlignment="Top" Width="120" SelectedDate="{Binding Path=DOB, UpdateSourceTrigger=PropertyChanged}"/>
            <Button Content="Add" HorizontalAlignment="Left" Margin="9,118,0,0" VerticalAlignment="Top" Width="75" Command="{Binding Path=AddCommand}"/>
            <Button Content="Update" HorizontalAlignment="Left" Margin="102,118,0,0" VerticalAlignment="Top" Width="75"/>
            <Button Content="Delete" HorizontalAlignment="Left" Margin="193,118,0,0" VerticalAlignment="Top" Width="75"/>
            <ListView BorderBrush="White" ItemsSource="{Binding Path=ContactsList, UpdateSourceTrigger=PropertyChanged}"
                       HorizontalAlignment="Stretch" Margin="11,156,10,10" SelectedValue="{Binding Path=Contacts, UpdateSourceTrigger=PropertyChanged}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="First Name" 
                                        DisplayMemberBinding="{Binding Path=First_Name}" Width="70" />
                        <GridViewColumn Header="Last Name"
                                        DisplayMemberBinding="{Binding Path=Last_Name}" Width="70" />
                        <GridViewColumn Header="DOB" 
                                        DisplayMemberBinding="{Binding Path=DOB}" Width="70" />
    
                    </GridView>
                </ListView.View>
            </ListView >
    
        </Grid>
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 2013-11-22
      • 2016-10-13
      相关资源
      最近更新 更多