【问题标题】:DataGridView not updating values on button click in WPFDataGridView 不更新 WPF 中按钮单击时的值
【发布时间】:2016-08-05 11:58:00
【问题描述】:

在我的应用程序中有两个窗口。主窗口包含一个 DataGridview 和一个按钮(添加)。单击按钮时,它会打开另一个窗口,其中包含 2 个文本框和按钮。

在窗口2,点击按钮时,文本框值需要发送并显示到主窗口DataGrid!

这是2个文件!..

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1(this);
            win.Show();
        }

    }

window1.cs

      public Window1()
            {
                InitializeComponent();     
            }
            private MainWindow m = null;
            public Window1(Window callingFrom)
            {
                m = callingFrom as MainWindow;
                InitializeComponent();
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Columns.Add("ID");
                DataRow dr = dt.NewRow();
                m.dataGrid1.ItemsSource = dt.DefaultView;
                m.dataGrid1.UpdateLayout();
            }
      private void btn_Click(object sender, RoutedEventArgs e)
            {
                DataView dv = m.dataGrid1.ItemsSource as DataView;

                DataTable dt = dv.Table;
                DataRow dr = dt.NewRow();
                dr["Name"] = txt1.Text;
                dr["ID"] = txt2.Text;
                dt.Rows.Add(dr);
               // this.Close();
                m.dataGrid1.UpdateLayout();
            }

}

问题是当关闭window1并再次打开window1以向Datagridview添加值时,主窗口的datagrid视图被替换而不是添加值!

(它正在一个一个地更新值以关闭窗口1)

如何解决这个问题!

谢谢!

【问题讨论】:

  • 我看到你反复问同一个问题而忽略了答案。

标签: c# wpf datagridview wpfdatagrid


【解决方案1】:

您创建了一个新的DataTable,其中没有任何内容。它是空的。然后您向其中添加了一项,并替换旧的ItemsSource 以查看新的DataTable,其中恰好包含一项新项。

当然,旧项目不再出现在网格中。为了摆脱它们,你费了很大的力气。

这样想:你有一个鞋盒,里面有一双鞋。你把它扔进垃圾桶,垃圾人把它带到垃圾场,它不见了,你再也看不到它了。理解:如果你扔掉盒子时鞋子还在盒子里,那么你就不再拥有那双鞋子了。他们带着盒子去了,因为他们在里面盒子里,盒子不见了,所以鞋子也不见了。

然后,第二天,你会得到一个新盒子,在里面放一双新鞋。

新盒子里有多少双鞋?正是一双鞋,那双是您刚刚放入盒子中的双,而不是放在另一个盒子中的旧双。当你扔掉它们所在的盒子时,你把旧的一对扔进了垃圾桶。

这有点像如果你上车开车去商店,当汽车到达商店时,你会带着它到达。因为您被困在车内。这只是一个类比:我并不是要暗示你个人实际上是一双被丢弃的鞋子,或者GridView中的一排。

以下是如何以较少错误的方式执行此操作:

public class GridItem 
{
    public GridItem(String i, String n)
    {
        ID = i;
        Name = n;
    }

    public String ID { get; protected set; }
    public String Name { get; protected set; }
}

public partial class MainWindow : Window
{
    public ObservableCollection<GridItem> GridItems { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        GridItems = new ObservableCollection<GridItem>();

        dataGrid1.ItemsSource = GridItems;
    }

    private void btn_Add_Click(object sender, RoutedEventArgs e)
    {
        Window1 win = new Window1(this);
        win.Show();
    }
}

public class Window1
{
    public Window1()
    {
        InitializeComponent();     
    }

    private MainWindow m = null;

    public Window1(Window callingFrom)
    {
        m = callingFrom as MainWindow;

        InitializeComponent();
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        //  Because m.GridItems is an ObservableCollection, which has been assigned 
        //  to the datagrid's ItemsSource property, the datagrid will magically 
        //  update itself with the new item. When you add new items, the old items will 
        //  still be seen because you are adding them to the same old collection 
        //  instead of creating a new empty collection every time. 
        m.GridItems.Add(new GridItem(txt2.Text, txt1.Text));
    }
}

有点离题,但你应该以有意义的方式命名。 txt2 没有任何意义。您应该将其命名为 txtID 或说明它是什么的名称,这样任何查看代码的人都可以立即理解它是什么。 m 也不是一个信息量很大的名称。

【讨论】:

  • 非常感谢!它按我的预期工作。!谢谢你们的cmets!我以后会关注那些东西的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 2016-11-24
  • 1970-01-01
相关资源
最近更新 更多