PC Review


Reply
Thread Tools Rate Thread

byte array in DataGridView

 
 
hajduk_25@yahoo.de
Guest
Posts: n/a
 
      21st Dec 2006
Hi,

my problem was already posted here in a similar manner, but I didn't
find a convincing solution for it yet. It's about the following: I use
in C# a DataGridView for displaying data, and one of the columns is a
byte array (it's a tinyblob in MySQL, I use it for simulating GUIDs in
MySQL, but this doesnt't matter). In the DataGridView this is always
shown as Byte[]-Array. One idea to solve this was to handle it in the
CellFormattingEvent like this:

private void DataGridContent_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e){
e.Value = bytearray_to_string((byte[])e.Value);
}
(bytearray_to_string is a method for converting a byte array into a
string)

This try led to a DataError, because e expected a byte array and not a
string as value. Then I tried to change the ValueType of the
DataGridView by inserting the codeline

((DataGridView)sender).Columns[e.ColumnIndex].ValueType =
typeof(string);

in the above code fragment. Surprisingly it was possible to change the
ValueType (I'd expected an error a la "changing ValueType is not
possible while the DataGridView contains data"), but the same error
occured again.

The latest try was to handle it via DataBinding:

Binding bi = new Binding("Text", Dt, "bytearraycolumn");
bi.Parse += new ConvertEventHandler(bi_Parse);
bi.Format += new ConvertEventHandler(bi_Format);

(Dt is the DataTable containing the data, the Parse and Format methods
convert byte arrays to strings and vice versa)

Unfortunally, the result was similar to the previous...

Has anybody an idea how to do this right?
Greetings from Germany
Hajduk

 
Reply With Quote
 
 
 
 
Bart Mermuys
Guest
Posts: n/a
 
      21st Dec 2006
Hi,

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> my problem was already posted here in a similar manner, but I didn't
> find a convincing solution for it yet. It's about the following: I use
> in C# a DataGridView for displaying data, and one of the columns is a
> byte array (it's a tinyblob in MySQL, I use it for simulating GUIDs in
> MySQL, but this doesnt't matter). In the DataGridView this is always
> shown as Byte[]-Array. One idea to solve this was to handle it in the
> CellFormattingEvent like this:
>
> private void DataGridContent_CellFormatting(object sender,
> System.Windows.Forms.DataGridViewCellFormattingEventArgs e){
> e.Value = bytearray_to_string((byte[])e.Value);
> }


Does it help if you add one (important) line :

private void DataGridContent_CellFormatting(object sender,
System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
{
// check right column first ???
e.Value = bytearray_to_string((byte[])e.Value);
e.FormattingApplied = true;
}

It shouldn't be a problem that ValueType is a byte[], FormattedValueType
should be typeof(string) which is the default for a TextboxColumn.

HTH,
Greetings


> (bytearray_to_string is a method for converting a byte array into a
> string)
>
> This try led to a DataError, because e expected a byte array and not a
> string as value. Then I tried to change the ValueType of the
> DataGridView by inserting the codeline
>
> ((DataGridView)sender).Columns[e.ColumnIndex].ValueType =
> typeof(string);
>
> in the above code fragment. Surprisingly it was possible to change the
> ValueType (I'd expected an error a la "changing ValueType is not
> possible while the DataGridView contains data"), but the same error
> occured again.
>
> The latest try was to handle it via DataBinding:
>
> Binding bi = new Binding("Text", Dt, "bytearraycolumn");
> bi.Parse += new ConvertEventHandler(bi_Parse);
> bi.Format += new ConvertEventHandler(bi_Format);
>
> (Dt is the DataTable containing the data, the Parse and Format methods
> convert byte arrays to strings and vice versa)
>
> Unfortunally, the result was similar to the previous...
>
> Has anybody an idea how to do this right?
> Greetings from Germany
> Hajduk
>



 
Reply With Quote
 
hajduk_25@yahoo.de
Guest
Posts: n/a
 
      21st Dec 2006
Hi Bart,

thanks for your hint, I tried it out, but it happened the same as
before. I got an error "The formatted value of the cell has a wrong
type".

So far, greetings to Netherlands (I think, you are from there...)

 
Reply With Quote
 
Bart Mermuys
Guest
Posts: n/a
 
      21st Dec 2006
Hi,

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Bart,
>
> thanks for your hint, I tried it out, but it happened the same as
> before. I got an error "The formatted value of the cell has a wrong
> type".


Weird, just tried a similar setup and it works ....

A couple of things to check :

- Are you sure you are using a TextBoxColumn, because the default for a
byte[] is an ImageColumn ?

- Don't change ValueType or FormattedValueType.

- CellFormatting is fired for cells in all columns, so are you checking it's
about the right column first.

- And, are you sure you're actually setting a string ?

>
> So far, greetings to Netherlands (I think, you are from there...)
>


No, but close :-)

HTH,
Greetings



 
Reply With Quote
 
hajduk_25@yahoo.de
Guest
Posts: n/a
 
      21st Dec 2006

> - Are you sure you are using a TextBoxColumn, because the default for a
> byte[] is an ImageColumn ?


No, the DataGridView uses as default a DataGridViewImageColumn. How can
I tell it to use a TextBoxColumn instead?

> - Don't change ValueType or FormattedValueType


I won't, I tried it once, but it was no good experience ...

> - CellFormatting is fired for cells in all columns, so are you checking it's
> about the right column first.


This is already done.

> - And, are you sure you're actually setting a string ?


More than sure

> >
> > So far, greetings to Netherlands (I think, you are from there...)
> >

>
> No, but close :-)


Belgium?!


Thanks again

 
Reply With Quote
 
Bart Mermuys
Guest
Posts: n/a
 
      21st Dec 2006
Hi,

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>> - Are you sure you are using a TextBoxColumn, because the default for a
>> byte[] is an ImageColumn ?

>
> No, the DataGridView uses as default a DataGridViewImageColumn. How can


Ah, it won't work with an DGVImageColumn.

> I tell it to use a TextBoxColumn instead?


Well that depends on how you are setting up the columns:

- if they've been setup at design time then open the column editor and
select the relevant column and then change ColumnType to DGVTextBoxColumn.

- if they are automatic setup at runtime by using DGV.AutoGenerateColumns,
then i'm not sure how to deal with this, if you can, add the columns using
the designer or manually from code, the most important column properties are
HeaderText and DataPropertyName.

>
>> - Don't change ValueType or FormattedValueType

>
> I won't, I tried it once, but it was no good experience ...
>
>> - CellFormatting is fired for cells in all columns, so are you checking
>> it's
>> about the right column first.

>
> This is already done.
>
>> - And, are you sure you're actually setting a string ?

>
> More than sure
>
>> >
>> > So far, greetings to Netherlands (I think, you are from there...)
>> >

>>
>> No, but close :-)

>
> Belgium?!


Yeah

HTH,
Greetings


>
>
> Thanks again
>



 
Reply With Quote
 
hajduk_25@yahoo.de
Guest
Posts: n/a
 
      21st Dec 2006

> - if they are automatic setup at runtime by using DGV.AutoGenerateColumns,
> then i'm not sure how to deal with this, if you can, add the columns using
> the designer or manually from code, the most important column properties are
> HeaderText and DataPropertyName.


So they are... I think, i'll return to my first quick-and-dirty
solution adding a TextBoxColumn, filling it with the desired string
computed from the byte array and replacing the byte array column by the
so created TextBoxColumn, it's not a very beautiful solution, but it
works...

Thanks again for your help
> > Belgium?!

>
> Yeah

good country, good beer (maneken )
so long and thanks for all the fish

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Value of type byte cannot be converted to 1-dimentional array of byte cmdolcet69 Microsoft VB .NET 3 25th Sep 2007 10:10 PM
How do I convert a ASCII Byte Array, to another Byte Array Russell Mangel Microsoft Dot NET Framework 2 2nd Feb 2005 06:01 PM
Re: Byte Array to Printable String to Byte Array Jon Skeet [C# MVP] Microsoft Dot NET 0 4th Aug 2004 01:53 PM
How to Convert Binary Coded Hex Byte Array to Byte Charles Law Microsoft VB .NET 25 2nd Jun 2004 02:21 PM
Convert native byte array (pointer) to managed byte[] Dave Microsoft Dot NET 1 13th Aug 2003 05:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.