CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2005
    Posts
    39

    Inserting a new row in datagridview

    I have a datagridview which is not bounded with any datasource.
    I have a Button.
    I want to add a new row to my datagridview on the button click.

    I tried the code:
    dgv.rows.add()

    and also
    dgv.rows.insert[index]

    but in both case the the row is being added to the row before last.

    Can any one help me??

    Thanks in advance

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Inserting a new row in datagridview

    I cannot reproduce your issue both :
    Code:
     dgv.Rows.Add();
    and

    Code:
     dgv.Rows.Insert(1);
    worked as desired. Can you perhaps elaborate more on what precisely is happening in your program, and when does this exactly happen ¿

  3. #3
    Join Date
    Mar 2006
    Posts
    38

    Re: Inserting a new row in datagridview

    I think you should create a table then
    create the desired columns and associated it with the table then add the row for example like the below code
    Code:
    //add a datagridview to your code -> dataGridView1
     
    DataTable tabel;
                    DataColumn nameColmn;
                    DataColumn mobileNoColmn;
                    DataColumn homeNoCol;
                    DataColumn workNoCol;
                    DataColumn emailColmn;
    
    tabel = new DataTable();
                nameColmn = tabel.Columns.Add("Name");
                homeNoCol = tabel.Columns.Add("Home");
                mobileNoColmn = tabel.Columns.Add("Mobile");
                workNoCol = tabel.Columns.Add("Work");
                emailColmn = tabel.Columns.Add("Email");
         
    //associate the tabel with datagrid view       
         dataGridView1.DataSource = tabel.DefaultView;
    
    create the row to add
    
    DataRow row = tabel.NewRow();
                            row[nameColmn] = "Falco Eyes";
                            row[homeNoCol] = "123456";
                            row[mobileNoColmn] = "";
                            row[workNoCol] = "";
                            row[emailColmn] = "falcon.eye71@gmail.com";
                            tabel.Rows.Add(row);
    Last edited by HanneSThEGreaT; May 13th, 2009 at 01:37 AM.

  4. #4
    Join Date
    May 2005
    Posts
    39

    Re: Inserting a new row in datagridview

    Quote Originally Posted by HanneSThEGreaT View Post
    I cannot reproduce your issue both :
    Code:
     dgv.Rows.Add();
    and

    Code:
     dgv.Rows.Insert(1);
    worked as desired. Can you perhaps elaborate more on what precisely is happening in your program, and when does this exactly happen ¿


    ----------------------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------------------------------

    For example my datagridview has 4 row.
    when I use the code:
    dgv.Rows.Add()
    A row is being added after third row (not after fourth row)

    when I use the code:
    dgv.Rows.Insert(4)
    An error occurs:
    "No row can be inserted after the uncommitted new row"

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Inserting a new row in datagridview

    Quote Originally Posted by opashafiq View Post
    ----------------------------------------------------------------------------------------------------------
    ----------------------------------------------------------------------------------------------------------

    For example my datagridview has 4 row.
    when I use the code:
    dgv.Rows.Add()
    A row is being added after third row (not after fourth row)

    when I use the code:
    dgv.Rows.Insert(4)
    An error occurs:
    "No row can be inserted after the uncommitted new row"
    I don't see the problem with Add() either, it appends the row to the end of the list. You cannot insert a row at 4 with only four rows present because you are inserting at the end, for which you use Add( ) and not insert.

  6. #6
    Join Date
    Aug 2009
    Posts
    1

    Re: Inserting a new row in datagridview

    Set 'AllowUserToAddRows' to false.

    The last row you see isn't a real row, it's there for people to manually enter a row

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured