Change color of Item Template TextBox on Condition

Discussion in 'ASP .Net Datagrid Control' started by Alan Z. Scharf, Aug 26, 2003.

  1. 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
     
    Alan Z. Scharf, Aug 26, 2003
    #1
    1. Advertising

  2. 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" <>
    | From: "Alan Z. Scharf" <>
    | 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: <>
    | 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
    |
    |
    |
     
    Lewis Wang [MSFT], Aug 27, 2003
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:...
    > 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" <>
    > | From: "Alan Z. Scharf" <>
    > | 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: <>
    > | 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
    > |
    > |
    > |
    >
     
    Alan Z. Scharf, Aug 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    697
    Remon van Vliet
    Jun 15, 2005
  2. Michael
    Replies:
    4
    Views:
    884
    Steven Cheng [MSFT]
    Jun 20, 2008
  3. fig000
    Replies:
    0
    Views:
    4,556
    fig000
    Sep 6, 2004
  4. Kamaljeet Saini
    Replies:
    0
    Views:
    430
    Kamaljeet Saini
    Feb 13, 2009
  5. Bill W.
    Replies:
    13
    Views:
    296
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page