0
votes

I am getting the following error:

DataGridView Default Error Dialog
The following exception occurred in the DataGridView:

System.ArgumentException: Parameter is not valid.

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)

at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)

at System.Drawing.Image.FromStream(Stream stream)

at System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)

at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)

at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)

at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) To replace this default dialog please handle the DataError event.

I have created a class

[DataContract]
public class Document : INotifyPropertyChanged
{
    [DataMember]
    private string _name;

    [DataMember]
    private DateTime _addedOn;

    [DataMember]
    private string _extension;

    [DataMember]
    private byte[] _content;

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public Document()
    {
        _name = String.Empty;
        _extension = String.Empty;
        _content = null;
        _addedOn = DateTime.Now;
    }

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    public DateTime AddedOn
    {
        get { return _addedOn; }
        set
        {
            _addedOn = value;
            OnPropertyChanged("AddedOn");
        }
    }

    public string Extension
    {
        get { return _extension; }
        set
        {
            _extension = value;
            OnPropertyChanged("Extension");
        }
    }

    public Byte[] Content
    {
        get { return _content; }
        set
        {
            _content = value;
            OnPropertyChanged("Content");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    /// <summary>
    /// Gets a value indicating if this document has already been submitted 
    /// to BancServ.
    /// </summary>
    public bool Processed
    {
        get
        {
            return (_content == null || _content.Length == 0);
        }
    }
}

I am binding a list of the above class to a datagridview and then I am getting the above error in the following section( it is a simple binding )

protected override void OnInitialize()
    {
        try
        {
            _documentsGridView.DataSource = this.Data.Documents;
            base.OnInitialize();
        }
        catch (Exception ex)
        {

        }
    }

    void btnBrowse_Click(object sender, EventArgs e)
    {
        try
        {
            Document document = this.Data.RequestDocument(FileKind.Browse, "Pdf files|*.pdf");
            this.Data.Documents.Add(document);
        }
        catch (Exception ex)
        {
        }
    }

Here I am browsing a *.pdf files and providing the info of the .pdf file in a datagridview by the list.

1
Please share the code that's actually causing the error, not just the implementation of your class.sab669

1 Answers

1
votes

This kind of error is too difficult to figure out from the info which you provided. Small hints I can give:

  • It is a datagridview binding issue. So the base of the error must be in binding.
  • Check the value of the properties which the datagridview is getting and analyse whether it should get those value or not.
  • Have you inserted DataGridViewColumn like(DataGridViewTextBoxColumn or DataGridViewCheckBoxColumn or DataGridViewComboBoxColumn) and binding those columns to some property of different value.
  • There could be some mismatch in binding the property.
  • If still you can not figure out the real cause then comment out all properties in class and keep only one property and check whether the error is coming or not. In this way check with each and every property one by one and comment at that time all other properties . Then you will be able to see for which property binding the error is coming.