datagrid problem

Discussion in 'ASP .Net Datagrid Control' started by Jack Tonk, Jan 26, 2004.

  1. Jack Tonk

    Jack Tonk Guest

    Hello. I have a simple datagrid with a column containing the last names of
    authors. When a condition is met, I'd like the column to contain a textbox
    which allows the user to change the last name. Otherwise, the cell will
    contain a label which displays the last name but won't allow any editing.

    The code is below. I keep getting Object reference not set to an instance
    of an object errors. It seems like this should work. Any ideas?
    Thanks in advance.

    The .aspx snippet:

    <asp:DataGrid ID="dgLastNames" Runat="server" AutoGenerateColumns="false"
    DataKeyField=UnProcessedID>
    <Columns>
    <asp:TemplateColumn HeaderText="Authors">
    <ItemTemplate>
    <asp:Label ID="lblLastName" Runat="server">last name</asp:Label>
    <asp:TextBox id="tbLastName" runat="server">last
    name</asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>

    The .aspx.vb snippet:

    Private Sub dgLastNames_ItemDataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    dgLastNames.ItemDataBound

    Dim lblLastName As Label
    lblLastName= CType(e.Item.FindControl("lblLastName"), Label)
    Dim tbLastName As TextBox
    tbLastName = CType(e.Item.FindControl("tbLastName"), TextBox)

    if somecondition = true then
    lblProductType.Visible = False
    tbLastName.Visible = true
    else
    lblProductType.Visible = true
    tbLastName.Visible = false
    end if
    End Sub
    Jack Tonk, Jan 26, 2004
    #1
    1. Advertising

  2. Hi Jack,
    Try it this way...

    Private Sub dgLastNames_ItemDataBound(sender As Object, e As
    DataGridItemEventArgs)
    Dim lblLastName As Label
    lblLastName= CType(e.Item.cells(0).FindControl("lblLastName"), Label)
    Dim tbLastName As TextBox
    tbLastName = CType(e.Item.cells(0).FindControl("tbLastName"), TextBox)

    if somecondition = true then
    lblProductType.Visible = False
    tbLastName.Visible = true
    else
    lblProductType.Visible = true
    tbLastName.Visible = false
    end if
    End sub


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Girish Prabhu, Jan 27, 2004
    #2
    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. sajvargs
    Replies:
    0
    Views:
    369
    sajvargs
    Feb 20, 2005
  2. DC
    Replies:
    2
    Views:
    595
  3. Andreas Klemt
    Replies:
    0
    Views:
    259
    Andreas Klemt
    Oct 8, 2003
  4. roberto
    Replies:
    1
    Views:
    210
    roberto
    Feb 17, 2005
  5. John Walsch
    Replies:
    0
    Views:
    186
    John Walsch
    Jul 9, 2004
Loading...

Share This Page