【发布时间】:2013-12-24 05:16:01
【问题描述】:
我正在尝试使用自定义对象的属性填充 datagridview,该自定义对象具有另一个自定义对象作为属性。:
class InventoryItem
{
public NItem Item { get; set; }
public int Quantity { get; set; }
}
class NItem
{
public String ItemNumber { get; set; }
public String Title { get; set; }
public double WholesalePrice { get; set; }
public double RetailPrice { get; set; }
public String Model { get; set; }
public string URL { get; set; }
}
我希望 datagridview 显示 NItem 中的每个属性和 Inventory Item 中的 Quantity。我正在使用的代码如下所示:
dataGridView1.DataSource = idr.GetAllItems();
idr 从数据库中读取项目并返回 InventoryItems 列表
提前感谢您的帮助。
编辑: 我发现我使用了一个数据表并遍历了生成唯一列的库存。代码:
// Create a new DataTable.
System.Data.DataTable table = new DataTable("InventoryTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create first column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "ItemNumber";
column.AutoIncrement = false;
column.Caption = "Item Number";
column.ReadOnly = true;
column.Unique = true;
// Add the column to the table.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Title";
column.AutoIncrement = false;
column.Caption = "Title";
column.ReadOnly = true;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Create third column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Model";
column.AutoIncrement = false;
column.Caption = "Model";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Create fourth column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Double");
column.ColumnName = "WholesalePrice";
column.AutoIncrement = false;
column.Caption = "Wholesale Price";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Create fifth column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Double");
column.ColumnName = "RetailPrice";
column.AutoIncrement = false;
column.Caption = "Retail Price";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Create sixth column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "Quantity";
column.AutoIncrement = false;
column.Caption = "Quantity";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Create seventh column.
//column = new DataColumn();
//column.DataType = System.Type.GetType("System.DateTime");
//column.ColumnName = "DatePurchased";
//column.AutoIncrement = false;
//column.Caption = "Date Purchased";
//column.ReadOnly = false;
//column.Unique = false;
//// Add the column to the table.
//table.Columns.Add(column);
// Create eigth column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "URL";
column.AutoIncrement = false;
column.Caption = "URL";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
foreach (var item in items)
{
row = table.NewRow();
row["ItemNumber"] = item.Item.ItemNumber;
row["Title"] = item.Item.Title;
row["Model"] = item.Item.Model;
row["WholesalePrice"] = item.Item.WholesalePrice;
row["RetailPrice"] = item.Item.RetailPrice;
row["Quantity"] = item.Quantity;
row["URL"] = item.Item.URL;
table.Rows.Add(row);
}
【问题讨论】:
-
您是否尝试过创建一个 List
并以这种方式将其绑定到网格......?您也可以将 DataGrid 绑定到 DataSet 或 DataView 或 DataTable 有这么多选项您还有什么尝试和/或调查过?研究以下内容 ObjectBindingSource
标签: c# datagridview datasource