Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Change color of Item Template TextBox on Condition

Reply
Thread Tools

Change color of Item Template TextBox on Condition

 
 
Alan Z. Scharf
Guest
Posts: n/a
 
      08-26-2003
1. I wantto change the color of a datagrid ItemTemplate TextBox in all the
grid rows depending on radio button choice.

2. How can I do this in C#?

So far I have:

public void dgHoldings_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(rdoHoldingsDisplay.SelectedItem.Value == "Inactive")
{
TextBox txtBoxHolding = new TextBox();
txtBoxHolding = (TextBox) e.Item.Cells[1].Controls[0];
txtBoxHolding.ForeColor = System.Drawing.Color.Red;
}
}

I get Invalid Cast error, but it doesn't work without cast either.

Any help would be appreciated.

Thanks.

Alan
___________________________
Alan Z. Scharf
GrapeVine Systems
New York City


 
Reply With Quote
 
 
 
 
Lewis Wang [MSFT]
Guest
Posts: n/a
 
      08-27-2003
Hi Alan,

In DataGird.ItemDataBound method, the first e.Item is the Header of the
Datagrid, the last Item is the Footer of the Datagrid. There is no Textbox
in the Header of the Datagrid. You may use "if((e.Item.ItemType ==
ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))"
to solve this issue. The following is a code snippet to demonstrate this.

void Item_Bound(Object sender, DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
ListItemType.AlternatingItem))
{
. . .
}
}

If you still get the "Invalid Cast error, then the problem should be that
the textbox in the Datagrid ItemTemplate is the second control of the cell.
So, you may use "e.Item.Cells[1].Controls[1]" to get the reference of the
textbox.

You may check the following links for your reference.
ItemDataBound Event (.NET Framework Class Library)
<http://msdn.microsoft.com/library/de...-us/cpref/html
/frlrfsystemwebuiwebcontrolsdatagridclassitemdatabo undtopic.asp>

DataGrid.UpdateCommand Event
<http://msdn.microsoft.com/library/de...-us/cpref/html
/frlrfsystemwebuiwebcontrolsdatagridclassupdatecomm andtopic.asp>

Hope this helps.

Best regards,
Lewis

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| Reply-To: "Alan Z. Scharf" <(E-Mail Removed)>
| From: "Alan Z. Scharf" <(E-Mail Removed)>
| Subject: Change color of Item Template TextBox on Condition
| Date: Tue, 26 Aug 2003 15:05:03 -0400
| Lines: 31
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.3718.0
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3718.0
| Message-ID: <(E-Mail Removed)>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
| NNTP-Posting-Host: 207-237-240-45.c3-0.nyw-ubr2.nyr-nyw.ny.cable.rcn.com
207.237.240.45
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridc ontrol:6357
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
|
| 1. I wantto change the color of a datagrid ItemTemplate TextBox in all
the
| grid rows depending on radio button choice.
|
| 2. How can I do this in C#?
|
| So far I have:
|
| public void dgHoldings_ItemDataBound(object sender,
| System.Web.UI.WebControls.DataGridItemEventArgs e)
| {
| if(rdoHoldingsDisplay.SelectedItem.Value == "Inactive")
| {
| TextBox txtBoxHolding = new TextBox();
| txtBoxHolding = (TextBox) e.Item.Cells[1].Controls[0];
| txtBoxHolding.ForeColor = System.Drawing.Color.Red;
| }
| }
|
| I get Invalid Cast error, but it doesn't work without cast either.
|
| Any help would be appreciated.
|
| Thanks.
|
| Alan
| ___________________________
| Alan Z. Scharf
| GrapeVine Systems
| New York City
|
|
|

 
Reply With Quote
 
 
 
 
Alan Z. Scharf
Guest
Posts: n/a
 
      08-29-2003
Lewis,

Thanks again for your reply.

I haven't had a chance to try your suggestion yet, but wanted to acknowledge
your reply.

Regards,

Alan

"Lewis Wang [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Alan,
>
> In DataGird.ItemDataBound method, the first e.Item is the Header of the
> Datagrid, the last Item is the Footer of the Datagrid. There is no Textbox
> in the Header of the Datagrid. You may use "if((e.Item.ItemType ==
> ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))"
> to solve this issue. The following is a code snippet to demonstrate this.
>
> void Item_Bound(Object sender, DataGridItemEventArgs e)
> {
> if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
> ListItemType.AlternatingItem))
> {
> . .
> }
> }
>
> If you still get the "Invalid Cast error, then the problem should be that
> the textbox in the Datagrid ItemTemplate is the second control of the

cell.
> So, you may use "e.Item.Cells[1].Controls[1]" to get the reference of the
> textbox.
>
> You may check the following links for your reference.
> ItemDataBound Event (.NET Framework Class Library)
>

<http://msdn.microsoft.com/library/de...-us/cpref/html
> /frlrfsystemwebuiwebcontrolsdatagridclassitemdatabo undtopic.asp>
>
> DataGrid.UpdateCommand Event
>

<http://msdn.microsoft.com/library/de...-us/cpref/html
> /frlrfsystemwebuiwebcontrolsdatagridclassupdatecomm andtopic.asp>
>
> Hope this helps.
>
> Best regards,
> Lewis
>
> This posting is provided "AS IS" with no warranties, and confers no

rights.
> --------------------
> | Reply-To: "Alan Z. Scharf" <(E-Mail Removed)>
> | From: "Alan Z. Scharf" <(E-Mail Removed)>
> | Subject: Change color of Item Template TextBox on Condition
> | Date: Tue, 26 Aug 2003 15:05:03 -0400
> | Lines: 31
> | X-Priority: 3
> | X-MSMail-Priority: Normal
> | X-Newsreader: Microsoft Outlook Express 6.00.3718.0
> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3718.0
> | Message-ID: <(E-Mail Removed)>
> | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
> | NNTP-Posting-Host: 207-237-240-45.c3-0.nyw-ubr2.nyr-nyw.ny.cable.rcn.com
> 207.237.240.45
> | Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
> | Xref: cpmsftngxa06.phx.gbl
> microsoft.public.dotnet.framework.aspnet.datagridc ontrol:6357
> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
> |
> | 1. I wantto change the color of a datagrid ItemTemplate TextBox in all
> the
> | grid rows depending on radio button choice.
> |
> | 2. How can I do this in C#?
> |
> | So far I have:
> |
> | public void dgHoldings_ItemDataBound(object sender,
> | System.Web.UI.WebControls.DataGridItemEventArgs e)
> | {
> | if(rdoHoldingsDisplay.SelectedItem.Value == "Inactive")
> | {
> | TextBox txtBoxHolding = new TextBox();
> | txtBoxHolding = (TextBox) e.Item.Cells[1].Controls[0];
> | txtBoxHolding.ForeColor = System.Drawing.Color.Red;
> | }
> | }
> |
> | I get Invalid Cast error, but it doesn't work without cast either.
> |
> | Any help would be appreciated.
> |
> | Thanks.
> |
> | Alan
> | ___________________________
> | Alan Z. Scharf
> | GrapeVine Systems
> | New York City
> |
> |
> |
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with while condition OR condition Bill W. Ruby 13 05-09-2011 09:42 PM
How to change the row color of the Repeater based on some condition? Michael ASP .Net 4 06-20-2008 06:29 AM
Hyperlink template column and change link text based on Condition Paul D. Fox ASP .Net Datagrid Control 3 06-16-2005 05:25 PM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
Problem with setting background color alternating item in datalist to a certain color fig000 ASP .Net Web Controls 0 09-06-2004 06:51 PM



Advertisments