Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Howdy!

Thanks for reading my post.

I have the following problem:
My program includes a DataGridView, which shows data from select statements supplied by the user. I use an OdbcDataAdapter to fill a DataTable, which is then assigned as the DataGridView's DataSource.

If the user supplies an SQL select statement which returns binary data, I get a DataError exception when the DataGridView tries to display the data.

How do I determine if a given column in the datatable includes binary data? I've tried using DataColumn.DataType, but binary data (which is of type Byte[]) isn't recognized (or I don't know what to compare to).

If the DataGridView showed a string representation of the binary data, or just a message stating 'binary data' in the column, that would work.

Thanks,

Jess
Posted
Updated 20-Oct-10 12:26pm
v2

I was able to use

C#
if (0 == String.Compare(dc.DataType.ToString().Trim(), "System.Byte[]"))


to determine the datatype. I stored it's table column index in a list<int>, and just deleted that column.


Thanks,

Jess

C#
List<int> theseAreBinaryData = new List<int>();

foreach (DataColumn dc in dt.Columns)
{
    if (0 == String.Compare(dc.DataType.ToString().Trim(), "System.Byte[]"))
    {
        theseAreBinaryData.Add(dt.Columns.IndexOf(dc.ColumnName));
    }
}
if (theseAreBinaryData.Count > 0)
{
    foreach (int i in theseAreBinaryData)
    {
        dt.Columns.RemoveAt(i);
    }
}
dataGridView.DataSource = dt;
 
Share this answer
 
v3
Comments
Toli Cuturicu 21-Oct-10 10:05am    
Don't post fake answers! This is blatant cheating!
AspDotNetDev 21-Oct-10 13:06pm    
I'm not sure what Toli is talking about, this seems like a working answer. Perhaps he is referring to the fact that you are the person asking the question and you are the person answering it. That is uncommon, but completely acceptable, even admirable that you took the time to come back here and suggest an answer. My guess is Toli is new here and is just a little confused. Keep up the good work, JessStuart.
JessStuart 21-Oct-10 13:30pm    
Howdy Toli and adpdotnetdev,
Thanks for the feedback.
@Toli: When I first posted this question, I didnt' know the answer. When I found out the answer (including code), I posted it to help the most people. I don't consider this cheating at all. I consider it helping people out (which is the point of this site). I'm not doing this to make points with anyone (is there even a way to do that?).
@apsdotnetdev: Thanks for the encouragement.
If you do a ToString() on the DataType, it should tell you what data type is used and you can go from there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900