DataGrid control with checkbox template column

Discussion in 'ASP .Net' started by Ryan Lafferty, Aug 8, 2005.

  1. Hi,

    I have a DataGrid control with a checkbox template column, and want to
    make the checkbox "disappear" when the 3rd column of the DataGrid
    contains the string "number". Is what I am trying to do impossible?

    This is what I have so far, but cannot get the darn thing to work:

    ------------------------------------------
    Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    myDataGrid.ItemDataBound

    Dim asdf As CheckBox
    asdf = CType(e.Item.FindControl("chk1"), CheckBox)

    If InStr(e.Item.Cells(3).Text, "Number") > 0 Then
    asdf.Visible = False
    Else
    asdf.Visible = True
    End If
    end Sub
    ------------------------------------------

    The code snippet above yields an "Object reference not set to an
    instance of an object." error. I am out of ideas....please help!
     
    Ryan Lafferty, Aug 8, 2005
    #1
    1. Advertising

  2. yeah, had this problem

    You have to ensure you're not in the Header or Footer template, otherwise
    the checkbox won't exist.

    I'm C#, so i'll put that code, then attempt the VB way

    Place this around your code

    if(e.Item.ItemTemplate != ListItemType.Header && e.Item.ItemTemplate !=
    ListItemType.Footer)
    {
    //do your logic here
    }

    So in VB:

    if e.Item.ItemTemplate <> ListItemType.Header AND e.Item.ItemTemplate <>
    ListItemType.Footer then
    'do your processing here
    end if


    That wasn't bad huh?



    "Ryan Lafferty" <> wrote in message
    news:...
    > Hi,
    >
    > I have a DataGrid control with a checkbox template column, and want to
    > make the checkbox "disappear" when the 3rd column of the DataGrid
    > contains the string "number". Is what I am trying to do impossible?
    >
    > This is what I have so far, but cannot get the darn thing to work:
    >
    > ------------------------------------------
    > Private Sub myDataGrid_ItemDataBound(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > myDataGrid.ItemDataBound
    >
    > Dim asdf As CheckBox
    > asdf = CType(e.Item.FindControl("chk1"), CheckBox)
    >
    > If InStr(e.Item.Cells(3).Text, "Number") > 0 Then
    > asdf.Visible = False
    > Else
    > asdf.Visible = True
    > End If
    > end Sub
    > ------------------------------------------
    >
    > The code snippet above yields an "Object reference not set to an
    > instance of an object." error. I am out of ideas....please help!
    >
     
    Grant Merwitz, Aug 8, 2005
    #2
    1. Advertising

  3. Thank you Grant, worked like a charm! Wasn't bad at all.


    BTW:
    e.Item.ItemTemplate (c#) = e.Item.ItemType (VB)
     
    Ryan Lafferty, Aug 8, 2005
    #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. Dexter
    Replies:
    1
    Views:
    419
    Eliyahu Goldin
    Jan 18, 2005
  2. Ken Varn
    Replies:
    1
    Views:
    978
    Ken Varn
    Aug 19, 2005
  3. Javier
    Replies:
    2
    Views:
    4,108
    Javier
    Dec 12, 2005
  4. NEMA
    Replies:
    0
    Views:
    783
  5. Gregory Rampton

    Convert an MS Access Yes/No column to a checkbox column in C# datagrid

    Gregory Rampton, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    199
    Gregory Rampton
    Aug 6, 2003
Loading...

Share This Page