public class PurchaseOrderItem
{
public Int64 PONumber { get; set; }
public string Description { get; set; }
public string UM { get; set; }
public int QTY { get; set; }
public decimal Cost { get; set; }
}
foreach (PurchaseOrderItem item in po.PurchaseOrderItems)
{
dgvPOItem.Rows.Add(item);
}
上面的Foreach不起作用。 我不能使用DataSource,因为我需要在添加数据后添加一个空行 因此,将来会有一个空行,用户可以在gridview上添加值。
答案 0 :(得分:1)
你不能使用BindingList<PurchaseOrderItem>
吗?
这应该允许您从dgv控件(使用空行)
编辑:我创建了简单的WinForm应用, 只有主要形式的DGV控制
Form1.cs代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); list.Add( new PurchaseOrderItem() { PONumber = 1, Description = "First item", UM = "something", QTY = 2341, Cost = 0.99M } ); dataGridView1.DataSource = list; dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler( dataGridView1_RowsAdded ); } void dataGridView1_RowsAdded( object sender, DataGridViewRowsAddedEventArgs e ) { object o = list; // added for breakpoint with variable viewing // you can watch your list changing here, when you add new rows } BindingList list = new BindingList(); } public class PurchaseOrderItem { public Int64 PONumber { get; set; } public string Description { get; set; } public string UM { get; set; } public int QTY { get; set; } public decimal Cost { get; set; } } }