Error in creating DataGridView rows on ADX panel

Add-in Express™ Support Service
That's what is more important than anything else

Error in creating DataGridView rows on ADX panel
 
This forum has moved to a new location. From now on, please post all your questions about Add-in Express Regions on .NET and VSTO forum.
Edward Murphy




Posts: 9
Joined: 2012-03-15
I am getting the following error. This happens after the code has created the DataGridView and inserted the rows successfully. The error occurs when the ADX panel is about to be shown on the right side of an Email item within an Outlook addin.

The error pops up three times, perhaps relating to the three columns in the DataGridView. A DataGridView inserting similar rows runs and shows successfully on a separate project built as a Windows Forms Application.

Here is the error, which I have typed (it shows within a message box where the text cannot be copied and pasted):

"DataGridView Default Error Dialog

The following exception occurred in the DataGridView:

System.FormatException: Value 'System.Object()' cannot be converted
to type 'Image'.
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."

Here is some code that results in this error:

Private Sub ADXOlForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' initialize DataGridView1
' Image
Dim ImageColumn As New DataGridViewImageColumn()
ImageColumn.Name = "ImageColumn"
ImageColumn.Width = 30
DataGridView1.Columns.Add(ImageColumn)
DataGridView1.Columns("ImageColumn").Visible = True

' Text
Dim TextColumn As New DataGridViewTextBoxColumn()
TextColumn.Name = "TextColumn"
TextColumn.Width = 169
DataGridView1.Columns.Add(TextColumn)
DataGridView1.Columns("TextColumn").Visible = True

' ID
Dim IDColumn As New DataGridViewTextBoxColumn()
TextColumn.Name = "IDColumn"
TextColumn.Width = 20
DataGridView1.Columns.Add(IDColumn)
DataGridView1.Columns("IDColumn").Visible = False

' dgv settings
DataGridView1.Columns.Item(1).DefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Gainsboro
DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black

Dim row As Object
row = {Image.FromFile("C:\Users\TedAdmin\Documents\Visual Studio 2010\Projects\Images\ctContact.png"), "Hello World", "Hello again"}
DataGridView1.Rows.Add(row)

End Sub
Posted 20 Mar, 2012 13:03:30 Top
Edward Murphy




Posts: 9
Joined: 2012-03-15
Tried loading the image from the Resources, but I got the same error message.

Dim row As Object
row = {My.Resources.ctContact, "Hello World", "Hello again"}
DataGridView1.Rows.Add(row)
Posted 20 Mar, 2012 20:29:41 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
Hello,

I wonder if this issue is reproducible on a System.Window.Forms.Form. If so, it doesn't relate to using our product.

Anyway, it looks like you need to find an appropriate walkthrough in this section on MSDN:

DataGridView Control (Windows Forms)

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Mar, 2012 02:01:06 Top
Edward Murphy




Posts: 9
Joined: 2012-03-15
I could not reproduce it on a System.Window.Forms.Form.

Here is how to reproduce this error using an AddIn Express region. The error seems to be about an image column on a DataGridView:

1. Create a new Outlook 2010 AddIn project
2. Add an ADX Region for Outlook and VSTO
3. Set the region to Inspector only, right side. Accept all other config defaults.
4. Drag a DataGridView on to the region's form.
5. Add the following sub to the ADXOlForm1 class:

Private Sub ADXOlForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim ImageColumn As New DataGridViewImageColumn()
DataGridView1.Columns.Add(ImageColumn)
Dim row As Object
row = {Image.FromFile("C:\Users\TedAdmin\Documents\Visual Studio 2010\Projects\Images\ctContact.png")}
DataGridView1.Rows.Add(row)

End Sub

6. Run the AddIn, open a Mail message, and you will see the error windows.
Posted 21 Mar, 2012 06:55:28 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
If you comment out the line adding that row, the code works okay. That is, the issue doesn't relate to using Add-in Express Regions for VSTO and Outlook, do you agree?

I suppose these walkthroughs shows how to display an image in a DataGridView column:

- How to: Display Images in Cells of the Windows Forms DataGridView Control
- How to: Customize Data Formatting in the Windows Forms DataGridView Control

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Mar, 2012 07:18:45 Top
Edward Murphy




Posts: 9
Joined: 2012-03-15
No, it looks to me like you can't use an image column within a DataGridView on an Addin Express Region. Let me try the two examples you have shown, thank you.
Posted 21 Mar, 2012 07:37:39 Top
Andrei Smolin


Add-in Express team


Posts: 14137
Joined: 2006-05-11
    Private Sub ADXOlForm1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim anIcon As New Icon("D:Images	est.ico")
        ' initialize DataGridView1
        ' Image
        Dim ImageColumn As New DataGridViewImageColumn()
        ImageColumn.Name = "ImageColumn"
        ImageColumn.Width = 30
        'ImageColumn.Image = anIcon.ToBitmap()
        DataGridView1.Columns.Add(ImageColumn)
        DataGridView1.Columns("ImageColumn").Visible = True

        ' Text
        Dim TextColumn As New DataGridViewTextBoxColumn()
        TextColumn.Name = "TextColumn"
        TextColumn.Width = 169
        DataGridView1.Columns.Add(TextColumn)
        DataGridView1.Columns("TextColumn").Visible = True

        '' ID
        'Dim IDColumn As New DataGridViewTextBoxColumn()
        'TextColumn.Name = "IDColumn"
        'TextColumn.Width = 20
        'DataGridView1.Columns.Add(IDColumn)
        'DataGridView1.Columns("IDColumn").Visible = False

        '' dgv settings
        'DataGridView1.Columns.Item(1).DefaultCellStyle.WrapMode = DataGridViewTriState.True
        'DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Gainsboro
        'DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black

        'Dim row As Object
        'row = {Image.FromFile("D:Imagescursor-classic.png"), "Hello World", "Hello again"}
        'DataGridView1.Rows.Add(row)
        'Dim row As DataRow()
        DataGridView1.Rows.Add(anIcon.ToBitmap(), "test")
    End Sub



This is how this POC works for me:

User added an image

I'm sure, you can polish this code to a desired degree.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 21 Mar, 2012 08:27:25 Top
Edward Murphy




Posts: 9
Joined: 2012-03-15
Thank you for your patience. I think that error resulted from putting string data into an image column in the DataGridView.
Posted 21 Mar, 2012 08:41:05 Top