How to add a row into GridView?

Discussion in 'ASP .Net Web Controls' started by Polaris, May 30, 2007.

  1. Polaris

    Polaris Guest

    Hi Experts:



    I'm using asp.net 2.0 with Visual Studio 2005. I'm trying to use the
    GridView to display data. I need to programmatically add rows into the
    GridView. So far could not find a way to do it. I appreciate it if any one
    can show a piece of code doing that.



    Thanks in advance!



    Polaris
     
    Polaris, May 30, 2007
    #1
    1. Advertising

  2. Polaris

    Polaris Guest

    Thanks, but I would not come to this group had I fund it in google.

    Polaris

    "Mark Rae" <> wrote in message
    news:...
    > "Polaris" <> wrote in message
    > news:...
    >
    >> I'm using asp.net 2.0 with Visual Studio 2005. I'm trying to use the
    >> GridView to display data. I need to programmatically add rows into the
    >> GridView. So far could not find a way to do it. I appreciate it if any
    >> one can show a piece of code doing that.

    >
    > Google is your friend:
    > http://www.google.co.uk/search?sour...GIH_en-GBGB220GB220&q=<asp:GridView> add rows
    >
    >
    > --
    > http://www.markrae.net
     
    Polaris, May 30, 2007
    #2
    1. Advertising

  3. Polaris

    Phil H Guest

    On 30 May, 20:00, "Polaris" <> wrote:
    > Hi Experts:
    >
    > I'm using asp.net 2.0 with Visual Studio 2005. I'm trying to use the
    > GridView to display data. I need to programmatically add rows into the
    > GridView. So far could not find a way to do it. I appreciate it if any one
    > can show a piece of code doing that.
    >
    > Thanks in advance!
    >
    > Polaris


    Hi Polaris

    Gridview does not have direct support for inserting records. It can
    only display the rows for data that already exists with a built-in
    means of editing and updating.

    It is possible to write code that will insert a blank record (with
    default column values in cases where nulls are not allowed) into the
    underlying data table, reload the grid with the databind method, then
    put the grid into edit mode with the editindex property set to the
    index of the new record. This is messy and complicated because your
    code would have to calculate the index value of the new record, which
    is not simple if the grid is paged. This may be why insert is not
    supported with GridView.

    It's much easier to use FormView for this purpose which only displayes
    one record at a time. FormView has an insert mode and makes it much
    simpler.

    Hence to achieve what you require (i.e. to add a means of inserting a
    new record in addition to displaying a list in a GridView control) you
    need both types of control in your application to handle the data. Add
    a button somewhere on the web form to signal that the user wishes to
    add a new record, then code the event handler to switch to displaying
    the FormView control (in insert mode) instead of the GridView. When
    the user clicks the button on the FormView to save the new record, use
    the ItemInserted event handler to switch back to displaying GridView
    (re-executing Databind if necessary).

    HTH

    P Hall
     
    Phil H, May 30, 2007
    #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. H5N1
    Replies:
    0
    Views:
    2,724
  2. Elmo Watson
    Replies:
    0
    Views:
    2,767
    Elmo Watson
    Aug 17, 2006
  3. Replies:
    5
    Views:
    668
  4. Polaris

    How to add a row into GridView?

    Polaris, May 30, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    698
    Phil H
    May 30, 2007
  5. Want to add row under selected row of gridview

    , Feb 16, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    304
Loading...

Share This Page