C#DataGridView将自定义对象添加为数据源

时间:2013-12-06 18:22:26

标签: c# datagridview datasource

我正在尝试使用另一个自定义对象作为属性的自定义对象的属性填充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 from Inventory Item。我正在使用的代码如下所示:

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);
        }

1 个答案:

答案 0 :(得分:0)

您应该尝试使用数据绑定和视图模型以及数据网格 假设你在XAML中有这样的观点

<UserControl
     User Control properties
     xmlns:vm="clr-namespace:ProjectName.ViewModel">

<UserControl.DataContext>
    <view:ClassName/>
</Userontrol.DataContext>

<Grid>
    <DataGrid ItemSource={"Binding InventoryItems"}/>

</Grid>
</UserControl>

假设您有一个InventoryItem ViewModel

Public class InventoryItemViewModel: INotifyPropertyChanged
{  

    public InventoryItemsViewModel()
    {
         PopulateInventoryItems();
    }
    public List<InventoryItem> _inventoryItems;

    private List<InventoryItem>
    { 
         get
         {
            return _inventoryItems;
         }
         set
         {
            _inventoryItems = value;
             RaiseProperyChanged();
         }
    }

    //Populate your List here

    Private PopulateInventoryItems()
    {

    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged([CallerMemberName] string PropertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }

}