Gridview - conditional formatting

N

Not Me

Hey,

In order to customise rows on my gridview control, I've some code in the
RowDataBound event, that works on each row, altering it's attributes..

for example I have:

If gvResults.DataKeys.Item(e.Row.RowIndex).Values.Item(1).ToString() =
"True" Then

e.Row.BackColor = Drawing.Color.LightGray
e.Row.ForeColor = Drawing.Color.Black
e.Row.Cells(1).Enabled = False

which tests a rows' hidden value (in a datakey) and acts accordingly on
the row... cells(1) holds a button.

Now I wish to do more to the row than just enable/disable it and change
colours.. supposing I wanted to change the text on the button - is this
possible?

If I do:
e.Row.Cells(1).Text = "Provide contact information"

it replaces the whole button with just some text.. how can I access the
buttons' .text property?

Cheers,
Chris
 
S

S. Justin Gengo [MCP]

Chris,

You need to get the button out of the row.

Dim RowButton as Button = CType(e.Row.FindControl("[ButtonId]"), Button)

RowButton.Text = "Provide contact information"


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
N

Not Me

S. Justin Gengo said:
Chris,

You need to get the button out of the row.

Dim RowButton as Button = CType(e.Row.FindControl("[ButtonId]"), Button)

RowButton.Text = "Provide contact information"

Super :)

Cheers,
Chris
 
N

Not Me

Not said:
S. Justin Gengo said:
Chris,

You need to get the button out of the row.

Dim RowButton as Button = CType(e.Row.FindControl("[ButtonId]"), Button)

RowButton.Text = "Provide contact information"

Super :)

And as per usual... I'm a tad hasty and need my hand held a little
further down the path!

The button comes from:

<asp:ButtonField Text="View Record" ButtonType="Button"
CommandName="select" />

....in the gridview columns section, so what would the buttonID be called?

Cheers for the help,
Chris
 
S

S. Justin Gengo [MCP]

Chris,

Ok, that is a bit different. I didn't know you were using an autogenerated
button. But things should be very similar. Instead of referring to the
button by its id refer to it by its index. And I think you'll find the
select button is actually a link button.

Dim RowButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Not Me said:
Not said:
S. Justin Gengo said:
Chris,

You need to get the button out of the row.

Dim RowButton as Button = CType(e.Row.FindControl("[ButtonId]"), Button)

RowButton.Text = "Provide contact information"

Super :)

And as per usual... I'm a tad hasty and need my hand held a little further
down the path!

The button comes from:

<asp:ButtonField Text="View Record" ButtonType="Button"
CommandName="select" />

...in the gridview columns section, so what would the buttonID be called?

Cheers for the help,
Chris
 
N

Not Me

S. Justin Gengo said:
Chris,

Ok, that is a bit different. I didn't know you were using an autogenerated
button. But things should be very similar. Instead of referring to the
button by its id refer to it by its index. And I think you'll find the
select button is actually a link button.

Dim RowButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

Thanks (again!), I've found the button, but it's type is
DataControlButton, and casting it as a Linkbutton gives a 'cannot cast'
error..

(Unable to cast object of type
'System.Web.UI.WebControls.DataControlButton' to type
'System.Web.UI.WebControls.LinkButton'.)

I can't find any information on DataControlButton anywhere!

Cheers,
Chris
 
S

S. Justin Gengo [MCP]

Chris,

Good to know that it's a datacontrol button. Just cast it as that and you
should be all set!

Sincerely,

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
N

Not Me

S. Justin Gengo said:
Chris,

Good to know that it's a datacontrol button. Just cast it as that and you
should be all set!

eeeh sorry, it threw me that using the datacontrolbutton gives errors in
my (beta) of visual studio (type not defined), I couldn't find mention
of the control in msdn either.

However when running on an IIS server using the release edition of .net
2.0, it must know what a datacontrolbutton is.. as the error I get is:
'System.Web.UI.WebControls.DataControlButton' is not accessible in this
context because it is 'Private'.

....just when I thought it was a nice thing that asp 2 writes half the
code for you :) any (more) ideas?

Cheers,
Chris
 
S

S. Justin Gengo [MCP]

Chris,

Ok, sorry I was away for a while here.

It looks as if you won't be able to use the built in button that's
automatically generated (big news eh?). But you should be able to do the
same thing by placing your own button column in the grid. You can do this
"codeless" by editing the grid's columns and dropping in a command button.
There you'll be able to set the command buttons to show or not to show and
they will be accessible because they will be set to public.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
Joined
Nov 8, 2006
Messages
1
Reaction score
0
There is still hope...

If you want to access the button, what you do is cast the control (which is the DataButtonControl) as an IButtonControl, if you want to access things like the text. If you want to access things like Enabled (which was in my case), simply safe cast it as a WebControl.

Easy as pie.
 

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

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top