byte array in DataGridView

H

hajduk_25

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
 
B

Bart Mermuys

Hi,

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
 
H

hajduk_25

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

Bart Mermuys

Hi,

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
 
H

hajduk_25

- 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 :)
No, but close :)

Belgium?!


Thanks again
 
B

Bart Mermuys

Hi,

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

hajduk_25

- 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
good country, good beer (maneken :))
so long and thanks for all the fish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top