Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6158823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:01:34+00:00 2026-05-23T21:01:34+00:00

If a DataSet contains a column that is a timestamp or other binary value,

  • 0

If a DataSet contains a column that is a timestamp or other binary value, its associated DataGridView throws an ArgumentException, when displaying any data in that column. That is, assume you have some table containing a binary column such as:

CREATE TABLE [dbo].[DataTest](
    [IdStuff] INT IDENTITY(1,1) NOT NULL,
    [ProblemColumn] TIMESTAMP NOT NULL )

In Visual Studio 2008, add a new Data Source pointing to the suspect table. Drag the table from the Data Source explorer onto the visual designer surface of a new WinForm to automatically create a DataGridView, BindingSource, etc. Execute the application and you will get a runtime exception. Sounds like a defect, right?

If you examine the Columns collection of the DataGridView you will find that it sets the column type to DataGridViewImageColumn. Why? Because, according to Microsoft, .NET assumes that binary columns are images. Indeed, Microsoft affirms that this behavior is by design! See this defect report on Microsoft Connect: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=93639

One could suppress the error dialog by handling the DataError event for the DataGridView, as the dialog politely indicates, but that begs the question. I want to find a way to avoid having an error condition in the first place. That is, I want to have a DataGridViewTextColumn showing a textual representation of the binary data, e.g. “0x1234a8e9433bb2”. And I am looking for a generic solution, since my actual code does not use a specific table as in my example above. Rather I put a somewhat arbitrary query into a dataAdapter.SelectCommand, then invoke

dataAdapter.Fill(dataTable)

to auto-generate my dataTable. Since it is the DataGridView that has the (IMHO) bug, I am thinking that I need to check the columns of the data table (i.e. dataTable.Columns[n].DataType.Name.Equals(“Byte[]”) ? ) and convert any byte arrays to their text forms manually before I connect the dataTable to the DataGridView with

bindingSource.DataSource = dataTable;

My question then:

Is there a simpler or more elegant way to display binary columns in a DataGridView?

(Note that this problem exists with both VS 2005 and VS 2008, .NET 2.0 and .NET 3.5.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:01:34+00:00Added an answer on May 23, 2026 at 9:01 pm

    Spurred on by Quandary’s answer, plus having allowed sufficient time since posting my question to have a fresh perspective :-), I came up with a reasonably clean solution in the guise of the MorphBinaryColumns method below, embedded in a complete sample test program (except for VS’s designer generated code from my WinForm containing a single DataGridView).

    MorphBinaryColumns examines the column collection and, for each that is a binary column,
    generates a new column with the value converted to a hex string, then swaps out the original column replacing it with the new one, preserving the original column order.

    public partial class Form1 : Form
    {
      public Form1()
      {
        InitializeComponent();
      }
    
      private void Form1_Load(object sender, EventArgs e)
      {
        var sqlCnn = new SqlConnection("..."); // fill in your connection string
        string strsql = "select ... from ..."; // fill in your query
    
        var dataAdapter = new SqlDataAdapter();
        var dataTable = new DataTable();
        dataAdapter.SelectCommand = new SqlCommand(strsql, sqlCnn);
        dataAdapter.Fill(dataTable);
        MorphBinaryColumns(dataTable);
        dataGridView1.DataSource = dataTable;
      }
    
      private void MorphBinaryColumns(DataTable table)
      {
        var targetNames =  table.Columns.Cast<DataColumn>()
          .Where(col => col.DataType.Equals(typeof(byte[])))
          .Select(col => col.ColumnName).ToList();
        foreach (string colName in targetNames)
        {
          // add new column and put it where the old column was
          var tmpName = "new";
          table.Columns.Add(new DataColumn(tmpName, typeof (string)));
          table.Columns[tmpName].SetOrdinal(table.Columns[colName].Ordinal);
    
          // fill in values in new column for every row
          foreach (DataRow row in table.Rows)
          {
            row[tmpName] = "0x" + string.Join("",
              ((byte[]) row[colName]).Select(b => b.ToString("X2")).ToArray());
          }
    
          // cleanup
          table.Columns.Remove(colName);
          table.Columns[tmpName].ColumnName = colName;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataSet that contains a few columns. One of these columns is
I have a dataset that I have modified into an xml document and then
I have a DataSet which I get a DataTable from that I am being
I have a dataset that has two tables in it. I want to do
I have a DataSet with a DataTable that correctly fills a single DataRow through
I have a large dataset (over 100,000 records) that I wish to load into
Here is the UI of my application. It contains a DataSet, a Save Button,
How much should one DataSet represent? Using the example of an ordering system: While
my SSRS DataSet returns a field with HTML, e.g. <b>blah blah </b><i> blah </i>.
I have a DataSet consisting of XML data, I can easily output this to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.