By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,385 Members | 847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,385 IT Pros & Developers. It's quick & easy.

Gridview - conditional formatting

P: n/a
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.Ite m(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
Feb 9 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
"Not Me" <no****@zxy.blah.org> wrote in message
news:ds**********@ucsnew1.ncl.ac.uk...
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.Ite m(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

Feb 9 '06 #2

P: n/a
S. Justin Gengo [MCP] wrote:
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
Feb 10 '06 #3

P: n/a
Not Me wrote:
S. Justin Gengo [MCP] wrote:
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

Feb 10 '06 #4

P: n/a
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" <no****@zxy.blah.org> wrote in message
news:ds**********@ucsnew1.ncl.ac.uk...
Not Me wrote:
S. Justin Gengo [MCP] wrote:
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

Feb 11 '06 #5

P: n/a
S. Justin Gengo [MCP] wrote:
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
Feb 14 '06 #6

P: n/a
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
"Not Me" <no****@zxy.blah.org> wrote in message
news:ds**********@ucsnew1.ncl.ac.uk...
S. Justin Gengo [MCP] wrote:
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

Feb 15 '06 #7

P: n/a
S. Justin Gengo [MCP] wrote:
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

Feb 17 '06 #8

P: n/a
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
"Not Me" <no****@zxy.blah.org> wrote in message
news:dt**********@ucsnew1.ncl.ac.uk...
S. Justin Gengo [MCP] wrote:
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

Feb 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.