Adding rows dynamically to gridview

Last post 10-23-2009 5:21 AM by Qin Dian Tang - MSFT. 6 replies.

Sort Posts:

  • Adding rows dynamically to gridview

    10-20-2009, 9:12 AM
    • Member
      70 point Member
    • sneha.
    • Member since 10-20-2009, 5:50 AM
    • Posts 19

    I have a gridview to which i want to add rows dynamically at any position just as in excel..

    example:

    If there are 10 rows in gridview and if i want to add a new row below or above 5th row.. how should i achieve it. 

    I don't want to do it by using gridview footer, as the row is getting added up at the bottom and not getting added up in between.

    i tried using insertat Position(...) .... but could not acheive it.

    Please  suggest a solution or any point on how to procced and acheive this.

     

    Please Mark as answer if this post helps you
  • Re: Adding rows dynamically to gridview

    10-20-2009, 9:40 AM
    • Member
      453 point Member
    • giveup
    • Member since 08-25-2009, 9:39 PM
    • Posts 131

    Hi,

    It is no matter how many rows or columns you have, what you need to do is like what the above solution is. Return data into a DataTable and insert new DataRow (empty data) into DataTable and rebind it to GridView. If you don't like the GridView makes too long due to the huge data source (255 rows), you can do paging in GridView. Just remember insert new row into the data source of GridView, not directly create a GridViewRow into GridView which is hard to implement.

    Some basic implementation of GridView can be found in the tutorial of this site: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/data/gridview.aspx

    Thanks!

    Please remember to click “Mark as Answer” on the post that helps you, it will help other(s) to get there answer.
  • Re: Adding rows dynamically to gridview

    10-20-2009, 9:53 AM
    • Contributor
      5,709 point Contributor
    • Pushkar
    • Member since 02-17-2006, 7:27 AM
    • Vibrant Gujarat
    • Posts 1,085

    Hi Sneha.

    I think u have to change ur requirement.

    As per knowledge it's not possible to add NewRow at any place in the GridView.

    You can add row from the footer or header.

    B'caz your gridview will be bound with some Unique number means Primary Number.

    Finally when u add the row,In the GridView it will come at the last page last record or first  record of first page as per ur sorting order.

    Hope u understand.

    “ There is no great genius without some touch of madness. ”


    Remember to click “Mark as Answer” on the post If you get answer from my post(s) !

    Thanks Guys
    ------------
    Pushkar M Rathod
  • Re: Adding rows dynamically to gridview

    10-20-2009, 10:16 AM
    • Contributor
      2,194 point Contributor
    • ratish_shriyan
    • Member since 10-05-2009, 8:55 AM
    • A D I T I Technologies, Bangalore
    • Posts 308

    Hi,

    The following article will help you,

    http://blog.falafel.com/2007/04/12/DynamicallyAddRowsToAGridView.aspx

    Good Luck!


    Knowledge Is Power
  • Re: Adding rows dynamically to gridview

    10-21-2009, 12:00 AM
    • Member
      70 point Member
    • sneha.
    • Member since 10-20-2009, 5:50 AM
    • Posts 19

    thanks for your valuable suggestions.....

    If you come accross any such kind of concept of adding rows inbetween.. please suggest me.. 

    Please Mark as answer if this post helps you
  • Re: Adding rows dynamically to gridview

    10-21-2009, 11:41 AM
    • Member
      636 point Member
    • lavanreddy
    • Member since 08-26-2009, 6:49 PM
    • Maryland
    • Posts 189

    Sneha,

    whats your requriement?

    Do you want to have a button called "Add new" in each row and clicking that opens a row below that

    or

    you want a button and a texbox at the top of the gridview (not header or footer), to provide the position of the new row by the user?


    Regards,
    Lavan
  • Re: Adding rows dynamically to gridview

    10-23-2009, 5:21 AM
    Answer

    Hi sneha.,

    Try this way:

    ChildTable table = GridView1.Rows[index].Parent as ChildTable;        //get that row
    if (table != null)
    {
        GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
        TableCell cell = new TableCell();
        cell.ColumnSpan = GridView1.Columns.Count;
        cell.Width = Unit.Percentage(100);
        cell.Controls.Add(new LiteralControl("New Row"));
        row.Cells.Add(cell);
        table.Rows.Add(row);
    }

    Thanks,

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (7 items)