Add a new record in GridView

Discussion in 'ASP .Net Datagrid Control' started by bruno, Sep 10, 2006.

  1. bruno

    bruno Guest

    I experimented inserting new rows in a table using a GridView (show footers,
    make all columns and commands into templatefields, add textboxes to the
    footer template of each column, add an Insert link in the footer, add a
    onInserting eventhandler…).

    Now, in case of an empty datasource I can't find the way to insert the first
    record. I tried to put textboxes in the EmptyDataTemplate but I found a lot
    of problems in getting user's input.

    The logic I'm trying to implement is:
    A first gridview displays rows from a first SQL table. Selecting a row from
    the first gridview, the second gridview shows related record from a second
    table.
    If there are no related records, how can I programmatically let the user
    insert the first row?

    Many Thanks.

    --
    bruno
    bruno, Sep 10, 2006
    #1
    1. Advertising

  2. Hi Bruno,

    What exactly problems are you getting when using EmptyDataTemplate?

    You may take a look at
    http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx for a
    working example on how to use this feature.

    Please tell me what do you think of this solution, I would be glad to
    continue work with you if you need anything else.

    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Sep 11, 2006
    #2
    1. Advertising

  3. bruno

    bruno Guest

    Hi Walter,
    from the working example you gave me, what is the VB.NET equivalent of
    Controls[0].Controls[0]?
    GridView1.Controls[0].Controls[0].GetType().Name or
    GridView1.Controls[0].Controls[0].FindControl("tbEmptyInsert")

    The problem I found in using EmptyDataTemplate is how to access labels and
    textboxes in that section, like I do in FooterRowTemplate.
    Thank you.
    --
    bruno


    "Walter Wang [MSFT]" wrote:

    > Hi Bruno,
    >
    > What exactly problems are you getting when using EmptyDataTemplate?
    >
    > You may take a look at
    > http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx for a
    > working example on how to use this feature.
    >
    > Please tell me what do you think of this solution, I would be glad to
    > continue work with you if you need anything else.
    >
    > Sincerely,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications. If you are using Outlook Express, please make sure you clear the
    > check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    > promptly.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    bruno, Sep 11, 2006
    #3
  4. Hi Bruno,

    Just use GridView1.Controls(0).Controls(0). Following is the complete code
    listing of VB.NET equivalent of the example:

    Imports System.Data


    Partial Class Default3
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
    Dim dt As New DataTable
    Dim dc As New DataColumn("Name")
    dt.Columns.Add(dc)
    Dim dr As DataRow = dt.NewRow()
    dr("Name") = "Ivan"

    GridView1.DataSource = dt
    GridView1.DataBind()
    End If

    RecurseControls(GridView1.Controls(0).Controls)
    Label1.Text += GridView1.Controls(0).Controls(0).GetType().Name +
    "<br />"
    End Sub

    Sub RecurseControls(ByVal ctls As ControlCollection)
    For Each ctl As Control In ctls
    If Not ctl.HasControls() Then
    Label1.Text += ctl.ClientID + " " + ctl.GetType().Name +
    "<br />"
    Else
    RecurseControls(ctl.Controls)
    End If
    Next
    End Sub

    Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    GridView1.RowCommand
    If e.CommandName = "EmptyInsert" Then
    Dim tbEmptyInsert As TextBox =
    GridView1.Controls(0).Controls(0).FindControl("tbEmptyInsert")
    Label1.Text = String.Format("You would have inserted the name:
    <b>{0}</b> from the emptydatatemplate", tbEmptyInsert.Text)
    End If
    If e.CommandName = "Insert" Then
    Dim tbInsert As TextBox =
    GridView1.FooterRow.FindControl("tbInsert")
    Label1.Text = String.Format("You would have inserted the name:
    <b>{0}</b> from the footerrow", tbInsert.Text)
    End If
    End Sub
    End Class

    I hope this helps. Please feel free to post here if anything is unclear.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Sep 12, 2006
    #4
  5. bruno

    bruno Guest

    I don't know way I found problems the first time I used
    GridView1.Controls(0).Controls(0) as you suggested me. it's my fault.
    Your sample helped me.
    Thank you Walter.
    --
    bruno


    "Walter Wang [MSFT]" wrote:

    > Hi Bruno,
    >
    > Just use GridView1.Controls(0).Controls(0). Following is the complete code
    > listing of VB.NET equivalent of the example:
    >
    > Imports System.Data
    >
    >
    > Partial Class Default3
    > Inherits System.Web.UI.Page
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > If Not IsPostBack Then
    > Dim dt As New DataTable
    > Dim dc As New DataColumn("Name")
    > dt.Columns.Add(dc)
    > Dim dr As DataRow = dt.NewRow()
    > dr("Name") = "Ivan"
    >
    > GridView1.DataSource = dt
    > GridView1.DataBind()
    > End If
    >
    > RecurseControls(GridView1.Controls(0).Controls)
    > Label1.Text += GridView1.Controls(0).Controls(0).GetType().Name +
    > "<br />"
    > End Sub
    >
    > Sub RecurseControls(ByVal ctls As ControlCollection)
    > For Each ctl As Control In ctls
    > If Not ctl.HasControls() Then
    > Label1.Text += ctl.ClientID + " " + ctl.GetType().Name +
    > "<br />"
    > Else
    > RecurseControls(ctl.Controls)
    > End If
    > Next
    > End Sub
    >
    > Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As
    > System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    > GridView1.RowCommand
    > If e.CommandName = "EmptyInsert" Then
    > Dim tbEmptyInsert As TextBox =
    > GridView1.Controls(0).Controls(0).FindControl("tbEmptyInsert")
    > Label1.Text = String.Format("You would have inserted the name:
    > <b>{0}</b> from the emptydatatemplate", tbEmptyInsert.Text)
    > End If
    > If e.CommandName = "Insert" Then
    > Dim tbInsert As TextBox =
    > GridView1.FooterRow.FindControl("tbInsert")
    > Label1.Text = String.Format("You would have inserted the name:
    > <b>{0}</b> from the footerrow", tbInsert.Text)
    > End If
    > End Sub
    > End Class
    >
    > I hope this helps. Please feel free to post here if anything is unclear.
    >
    > Regards,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    bruno, Sep 12, 2006
    #5
  6. If there is no row matched Add a Empty row to the second table dataset and
    bind the dataset to Datagrid now the new blank row will be added ..




    "bruno" wrote:

    > I don't know way I found problems the first time I used
    > GridView1.Controls(0).Controls(0) as you suggested me. it's my fault.
    > Your sample helped me.
    > Thank you Walter.
    > --
    > bruno
    >
    >
    > "Walter Wang [MSFT]" wrote:
    >
    > > Hi Bruno,
    > >
    > > Just use GridView1.Controls(0).Controls(0). Following is the complete code
    > > listing of VB.NET equivalent of the example:
    > >
    > > Imports System.Data
    > >
    > >
    > > Partial Class Default3
    > > Inherits System.Web.UI.Page
    > >
    > > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > > System.EventArgs) Handles Me.Load
    > > If Not IsPostBack Then
    > > Dim dt As New DataTable
    > > Dim dc As New DataColumn("Name")
    > > dt.Columns.Add(dc)
    > > Dim dr As DataRow = dt.NewRow()
    > > dr("Name") = "Ivan"
    > >
    > > GridView1.DataSource = dt
    > > GridView1.DataBind()
    > > End If
    > >
    > > RecurseControls(GridView1.Controls(0).Controls)
    > > Label1.Text += GridView1.Controls(0).Controls(0).GetType().Name +
    > > "<br />"
    > > End Sub
    > >
    > > Sub RecurseControls(ByVal ctls As ControlCollection)
    > > For Each ctl As Control In ctls
    > > If Not ctl.HasControls() Then
    > > Label1.Text += ctl.ClientID + " " + ctl.GetType().Name +
    > > "<br />"
    > > Else
    > > RecurseControls(ctl.Controls)
    > > End If
    > > Next
    > > End Sub
    > >
    > > Protected Sub GridView1_RowCommand1(ByVal sender As Object, ByVal e As
    > > System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    > > GridView1.RowCommand
    > > If e.CommandName = "EmptyInsert" Then
    > > Dim tbEmptyInsert As TextBox =
    > > GridView1.Controls(0).Controls(0).FindControl("tbEmptyInsert")
    > > Label1.Text = String.Format("You would have inserted the name:
    > > <b>{0}</b> from the emptydatatemplate", tbEmptyInsert.Text)
    > > End If
    > > If e.CommandName = "Insert" Then
    > > Dim tbInsert As TextBox =
    > > GridView1.FooterRow.FindControl("tbInsert")
    > > Label1.Text = String.Format("You would have inserted the name:
    > > <b>{0}</b> from the footerrow", tbInsert.Text)
    > > End If
    > > End Sub
    > > End Class
    > >
    > > I hope this helps. Please feel free to post here if anything is unclear.
    > >
    > > Regards,
    > > Walter Wang (, remove 'online.')
    > > Microsoft Online Community Support
    > >
    > > ==================================================
    > > When responding to posts, please "Reply to Group" via your newsreader so
    > > that others may learn and benefit from your issue.
    > > ==================================================
    > >
    > > This posting is provided "AS IS" with no warranties, and confers no rights.
    > >
    > >
    Prabakar Samiyappan, Mar 10, 2007
    #6
    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. André
    Replies:
    0
    Views:
    3,727
    André
    Jun 25, 2006
  2. Frits van Soldt

    How to insert a new record in detailsview when there are no record

    Frits van Soldt, Nov 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    717
    Frits van Soldt
    Nov 17, 2005
  3. André
    Replies:
    0
    Views:
    433
    André
    Jun 25, 2006
  4. Replies:
    0
    Views:
    159
  5. Replies:
    2
    Views:
    207
Loading...

Share This Page