Change color of Item Template TextBox on Condition

A

Alan Z. Scharf

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
 
L

Lewis Wang [MSFT]

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/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwebuiwebcontrolsdatagridclassitemdataboundtopic.asp>

DataGrid.UpdateCommand Event
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwebuiwebcontrolsdatagridclassupdatecommandtopic.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" <[email protected]>
| From: "Alan Z. Scharf" <[email protected]>
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| 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!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:6357
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 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
|
|
|
 
A

Alan Z. Scharf

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 said:
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/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwebuiwebcontrolsdatagridclassitemdataboundtopic.asp>

DataGrid.UpdateCommand Event
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html
/frlrfsystemwebuiwebcontrolsdatagridclassupdatecommandtopic.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" <[email protected]>
| From: "Alan Z. Scharf" <[email protected]>
| 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: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| 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!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:6357
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 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
|
|
|
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top