Gridview - conditional formatting

  • Thread starter Thread starter Not Me
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
Back
Top