简体   繁体   English

如何在wpf中向datagrid添加行?

[英]How can I add row to datagrid in wpf?

I am very new to wpf. 我对wpf很陌生。 I am looking for some equivalent for: 我正在寻找一些等效的:

dataGridView1.Rows.Add(sNazwa, 1, opakowanie, cena, sWartoscNetto, sVAT, sWartoscBrutto);

I know that I should use 我知道我应该使用

mainwindow.dataGrid1.Items.Add()

but how can I define a row I want to add here? 但是如何定义要在此处添加的行?

EDIT 编辑

I think I should elaborate a bit on what I plan to achieve. 我想我应该详细说明我打算实现的目标。 I am rewriting a program I made in WinForms. 我正在重写在WinForms中制作的程序。 It makes offers for clients. 它为客户提供报价。 It has a gridview in winforms that has a list of products for my client. 它具有winforms中的gridview,其中包含我的客户的产品列表。 In secon window I have a gridform connected to sql database. 在secon窗口中,我有一个连接到sql数据库的网格窗体。 By double click on a row, some of data from the row should be transfered to mainwindow datagrid (but not the whole row). 通过双击一行,该行中的某些数据应传输到mainwindow数据网格中(而不是整个行)。 In mainwindow datagrid user should be able to edit cells, move rows (up and down), sort and delete rows. 在mainwindow中,datagrid用户应该能够编辑单元格,上下移动行,对行进行排序和删除。 Will List<> let me do all this things? List <>可以让我做所有这些事情吗? I tried to use your solution to add a row using List and it works (but I struggle to send data from second window), but still I wonder if it will let me do al this things I want later on. 我尝试使用您的解决方案通过List添加行,并且行有效(但是我很难从第二个窗口发送数据),但是我仍然想知道它是否可以让我做以后想要的事情。

EDIT This works: 编辑这工作:

public class Produkt
{
  public string Nazwa { get; set;}
  public string Opakowanie { get; set; }
  public string Ilosc { get; set; }
  public string Cena { get; set; }
  public string WartoscNetto { get; set; }
  public string VAT { get; set; }
  public string WartoscBrutto { get; set; }
}

public List<Produkt> DodajProdukty()
{
  List<Produkt> produkt = new List<Produkt>();
  produkt.Add(new Produkt() { Nazwa = "test", Opakowanie = "test", Ilosc = "1", Cena = "test", WartoscNetto = "test", WartoscBrutto = "test" });
  return produkt;

}


private void Window_Loaded(object sender, RoutedEventArgs e)
{
  dataGrid1.ItemsSource = DodajProdukty();
}

but how can I add a row from different window? 但是如何从其他窗口添加一行?

thanks, 谢谢,

As pointed out earlier by HighCore , you should do that by using a list (collection) of objects, you want to display in your DataGrid. 正如HighCore先前指出的那样,您应该通过使用要显示在DataGrid中的对象列表(集合)来执行此操作。 This is where Binding comes in. 这是绑定进入的地方。

There are several tutorials and guides out there but you may have some questions or need some hints to carry on. 那里有一些教程和指南,但是您可能有一些疑问或需要一些提示来进行。

At this moment it'd good to know that some things are quite different while others are not when passing over to WPF. 目前,很高兴知道有些事情完全不同,而有些事情在传递给WPF时并没有什么不同。 You may read this article written by Reed Copsey . 您可以阅读Reed Copsey撰写的这篇文章。 It was a really great read and helped me leaving WinForms as framework behind. 这是一本非常好的书,它帮助我将WinForms留作框架。

If you are at the point where you'd say that you are ready to implement your first MVVM sample, you should really take a look into concepts such as Binding , Events , Commands and how it works first. 如果您已准备好实施第一个MVVM示例,则应该真正研究一下诸如BindingEventsCommands以及它们如何首先工作的概念。

In this particular case you wouldn't need the functionality to notify the View about any changes that have been made since you stated above, that the data won't change. 在这种特殊情况下,您不需要自上次声明以来就不会对数据进行任何更改的通知View的功能。 However the interface INotifyPropertyChanged isn't necessary at all here but you should consider using it in though. 但是,这里完全不需要INotifyPropertyChanged接口,但是您应该考虑使用它。

Ok, this worked perfectly: 好的,这很完美:

https://stackoverflow.com/a/15749641/2186128 https://stackoverflow.com/a/15749641/2186128

Now I need to learn what all that mean :) 现在我需要了解所有的意思:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM