DataGridView - problem with custom coloring

  • Thread starter Thread starter Katit
  • Start date Start date
K

Katit

I need to be able to custom color background of cells.

When I do

DataGridViewCell.Style.BackColor = Color.Yellow;

It works properly as with any other enumerated color.

When I try to do something like:

DataGridViewCell.Style.BackColor =
Color.FromArgb(Convert.ToInt32("68FDFA", 16));

It makes it like transparent and then it's all messed up. Hard to
explain, but it doesn't work. It get's impring of underlying window.

Anybody had this problem before? How do I create custom colors by
knowing HEX color value?
In VB6 I used FarPoint grid and did something like:

..BackColor = Val("&h" & "68FDFA")

Basically, it was taking long value...

What is the problem here? I'm all confused..
 
[...]
DataGridViewCell.Style.BackColor =
Color.FromArgb(Convert.ToInt32("68FDFA", 16));

It makes it like transparent and then it's all messed up. Hard to
explain, but it doesn't work. It get's impring of underlying window.

I admit, I'm not sure why the Int32 parsing isn't going as expected. To
my sleep-deprived brain, it looks okay to me.

However, what about trying Color.FromArgb(0x0068FDFA)?

The fact that the color winds up transparent suggests that somehow the
alpha channel is getting set in the 32-bit int you've parsed. Again, I
don't see why that's happening, but if you specify the ARGB value
explicitly as a number, I would expect that would avoid whatever's going
on with the parse.

Pete
 
[...]
DataGridViewCell.Style.BackColor =
Color.FromArgb(Convert.ToInt32("68FDFA", 16));
It makes it like transparent and then it's all messed up. Hard to
explain, but it doesn't work. It get's impring of underlying window.

I admit, I'm not sure why the Int32 parsing isn't going as expected. To
my sleep-deprived brain, it looks okay to me.

However, what about trying Color.FromArgb(0x0068FDFA)?

The fact that the color winds up transparent suggests that somehow the
alpha channel is getting set in the 32-bit int you've parsed. Again, I
don't see why that's happening, but if you specify the ARGB value
explicitly as a number, I would expect that would avoid whatever's going
on with the parse.

Pete

Weird. Just tried:

get {
//return Color.FromArgb(0x0074FA85);
return Color.Yellow;
}

commented line didn't work, other line did. What do I do now?...
It's not transparent as it shows stuff when moving form. It's
transparent only when painted..
 
[...]
DataGridViewCell.Style.BackColor =
Color.FromArgb(Convert.ToInt32("68FDFA", 16));
It makes it like transparent and then it's all messed up. Hard to
explain, but it doesn't work. It get's impring of underlying window.

I admit, I'm not sure why the Int32 parsing isn't going as expected. To
my sleep-deprived brain, it looks okay to me.

However, what about trying Color.FromArgb(0x0068FDFA)?

The fact that the color winds up transparent suggests that somehow the
alpha channel is getting set in the 32-bit int you've parsed. Again, I
don't see why that's happening, but if you specify the ARGB value
explicitly as a number, I would expect that would avoid whatever's going
on with the parse.

Pete

Ok! Got it. Your mistake is that A is "00" It should be "FF" for non-
transparent.
However, conversion will not work due to signed type of Int32 and
UInt32 is invalid for FromArgb function.

I ended up doing:
get { return Color.FromArgb(255,
Color.FromArgb(Convert.ToInt32("68FDFA", 16)) ); }
 
Ok! Got it. Your mistake is that A is "00" It should be "FF" for non-
transparent.

My mistake? That's your mistake. I just inherited it. :p I did point
out that I'm sleep-deprived.

But yes, you are right...for an opaque color the alpha value actually
should be 255, not 0.
However, conversion will not work due to signed type of Int32 and
UInt32 is invalid for FromArgb function.

So just use the explicit hexadecimal constant: 0xff68fdfa.

Alternatively, just use Color.FromArgb(0x68, 0xfd, 0xfa), which implicitly
sets the alpha to opaque.
I ended up doing:
get { return Color.FromArgb(255,
Color.FromArgb(Convert.ToInt32("68FDFA", 16)) ); }

I suppose that's another way to do it. Not sure why you insist on going
through the In32 parser at run-time, but okay. I just hope you don't need
to get this property very often.

Pete
 
My mistake? That's your mistake. I just inherited it. :p I did point
out that I'm sleep-deprived.

But yes, you are right...for an opaque color the alpha value actually
should be 255, not 0.


So just use the explicit hexadecimal constant: 0xff68fdfa.

Alternatively, just use Color.FromArgb(0x68, 0xfd, 0xfa), which implicitly
sets the alpha to opaque.


I suppose that's another way to do it. Not sure why you insist on going
through the In32 parser at run-time, but okay. I just hope you don't need
to get this property very often.

Pete

I do it because those colors will be in INI file as text values at
some point.
 

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

Back
Top