dcsimg
Results 1 to 4 of 4

Thread: DataGridView data type error

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    2

    DataGridView data type error

    Hello,

    I am trying to populate a datagridview with data from an sql query. I've done this many times before, but one column in this data is being identified as an image when it's really not.

    My code:
    Dim sql As String = "select col1, col2, col3, col4 from datasource where col1 = 'yada'"
    Using cn As New SqlConnection("Data Source=SERVER;Persist Security Info=True;User ID=" & NOTREAL& ";Password=" & STILLNOTREAL), _
    cmd As New SqlCommand(sql, cn)
    cn.Open()
    Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
    Dim myDataSet As DataSet = New DataSet()
    myDA.Fill(myDataSet, "tableName")
    If myDataSet.Tables.Count > 0 Then
    If myDataSet.Tables("tableName").Rows.Count > 0 Then
    DataGridView1.DataSource = myDataSet.Tables("tableName").DefaultView
    End If
    End If
    cn.Close()
    End Using


    In this example, the data for col4 is an OID. (Example: 0x82A6EDEGG0030080)
    An exception is thrown for every row, and as they are acknowledged, the row displays all the data normally but col4, which displays a red x in a box like it thinks it's an image.

    The error being thrown is as follows:

    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.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)


    How do I tell the datagridview that col4 should be displayed as text?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: DataGridView data type error

    Please format code snippets for readability.
    Code:
            Dim sql As String = "select col1, col2, col3, col4 from datasource where col1 = 'yada'"
            Using cn As New SqlConnection("Data Source=SERVER;Persist Security Info=True;User ID=" & NOTREAL& ";Password=" & STILLNOTREAL), _
                cmd As New SqlCommand(sql, cn)
                cn.Open()
                Dim myDA As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim myDataSet As DataSet = New DataSet()
                myDA.Fill(myDataSet, "tableName")
                If myDataSet.Tables.Count > 0 Then
                    If myDataSet.Tables("tableName").Rows.Count > 0 Then
                        DataGridView1.DataSource = myDataSet.Tables("tableName").DefaultView
                    End If
                End If
                cn.Close()
            End Using

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: DataGridView data type error

    Firstly, there's no point creating a DataSet if all you need is a DataTable and there's no point binding the DefaultView because that's where the data comes from when you bind a DataTable anyway. Just create a DataTable, Fill it and then bind it.

    As for the issue, if you rely on the grid creating the columns for you then it will interpret binary data as images. If that's not what you want then you need to create at least one column yourself in the designer. You have three choices:

    1. If you don't want that column bound, create all the columns you do want and set their DataPropertyName to control binding. Set AutoGenerateColumns to False before binding.

    2. If you do want that column bound, create a column of the type you want for that source column and set its DataPropertyName appropriately. When you bind, the other columns will be created automatically.

    3. If you do want that column bound, create all the columns you do want and set their DataPropertyName to control binding.

    Personally, I would go for #1 or #3. If you go for #2 then you may need to fiddle with column ordering after binding.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    2

    Resolved Re: DataGridView data type error

    Quote Originally Posted by jmcilhinney View Post
    Firstly, there's no point creating a DataSet if all you need is a DataTable and there's no point binding the DefaultView because that's where the data comes from when you bind a DataTable anyway. Just create a DataTable, Fill it and then bind it.

    As for the issue, if you rely on the grid creating the columns for you then it will interpret binary data as images. If that's not what you want then you need to create at least one column yourself in the designer. You have three choices:

    1. If you don't want that column bound, create all the columns you do want and set their DataPropertyName to control binding. Set AutoGenerateColumns to False before binding.

    2. If you do want that column bound, create a column of the type you want for that source column and set its DataPropertyName appropriately. When you bind, the other columns will be created automatically.

    3. If you do want that column bound, create all the columns you do want and set their DataPropertyName to control binding.

    Personally, I would go for #1 or #3. If you go for #2 then you may need to fiddle with column ordering after binding.
    Thanks for the help. I went with 3, but I'm thinking I'm going to actually just convert it in the sql to varchar. No idea why I didn't think of that yesterday.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width