Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2006

Tip: Looking for answers? Try searching our database.

byte array in DataGridView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hajduk_25@yahoo.de - 21 Dec 2006 10:36 GMT
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
Bart Mermuys - 21 Dec 2006 12:01 GMT
Hi,

> Hi,
>
[quoted text clipped - 10 lines]
>      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)
[quoted text clipped - 25 lines]
> Greetings from Germany
> Hajduk
hajduk_25@yahoo.de - 21 Dec 2006 12:25 GMT
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...)
Bart Mermuys - 21 Dec 2006 13:35 GMT
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
hajduk_25@yahoo.de - 21 Dec 2006 14:04 GMT
> - 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
Bart Mermuys - 21 Dec 2006 14:50 GMT
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.

>> - Don't change ValueType or FormattedValueType
>
[quoted text clipped - 15 lines]
>
> Belgium?!

Yeah

HTH,
Greetings

> Thanks again
hajduk_25@yahoo.de - 21 Dec 2006 15:14 GMT
> - 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.