Kevin
Kevin

Reputation: 139

Parameter is not valid datagridview default error dialog

BindingList<Item> itemList = new BindingList<Item>();
        foreach (Item item in po.Items)
        {
            itemList.Add(item);
        }
        ==> dgvItems.DataSource = itemList;
        dgvItems.Columns["ItemId"].Visible = false;

When I run my program it throws the error at the line I marked with "==>" I don't know where the error is coming from because when I debug the list contains valid data. It was working before so I think the error appears since I did some changes to the timestamp repository code.

This is my Item object definition:

public class Item
{
    public byte[] TimeStamp { get; set; }

    public int ItemId { get; set; }

    [Required(ErrorMessage = "Item Name is required")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Description is required")]
    public string Description { get; set; }

    [Required(ErrorMessage = "Price is required")]
    public decimal Price { get; set; }

    [Required(ErrorMessage = "Location is required")]
    public string Location { get; set; }

    //public byte[] TimeStamp { get; set; }

    [Required(ErrorMessage = "Justification is required")]
    public string Justification { get; set; }

    [Required(ErrorMessage = "Quantity is required")]
    public int Quantity { get; set; }

    public int PurchaseOrderId { get; set; }

    public DateTime Date { get; set; }

    public ItemStatus Status { get; set; }

    public Item()
    {

    }

    public Item(int itemId, string name, string description, decimal price, string location, string justification, int quantity, int purchaseOrderId, DateTime date, ItemStatus status)
    {
        ItemId = itemId;
        Name = name;
        Description = description;
        Price = price;
        Location = location;
        Justification = justification;
        Quantity = quantity;
        PurchaseOrderId = purchaseOrderId;
        Date = date;
        Status = status;
    }
}

Upvotes: 0

Views: 302

Answers (1)

Jimi
Jimi

Reputation: 32278

Here a couple of option, see what better fits.

Option 1:

Add a [Browsable(false)] attribute to the Item class object:
The DataGridView won't generate a Column for TimeStamp property.

To note that:

Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public class Item
{
    [Browsable(false)]
    public byte[] TimeStamp { get; set; }
    public int ItemId { get; set; }
    [Required(ErrorMessage = "Item Name is required")]
    public string Name { get; set; }
    //[...]
}

Option 2:

Remove the Column right after the DataGridView.DataSource has been set:

To note that you won't be able to modify the TimeStamp value through the DataGridView. But, as mentioned in comments, this property doesn't need to be modified.

var itemList = new BindingList<Item>(po.Items);
dataGridView1.DataSource = itemList;
dataGridView1.Columns.Remove("TimeStamp");

Upvotes: 1

Related Questions