-
May 12th, 2009, 02:43 AM
#1
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
-
May 12th, 2009, 11:22 AM
#2
Re: Inserting a new row in datagridview
I cannot reproduce your issue both :
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 ¿
-
May 12th, 2009, 04:17 PM
#3
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.
-
May 13th, 2009, 12:15 AM
#4
Re: Inserting a new row in datagridview
Originally Posted by HanneSThEGreaT
I cannot reproduce your issue both :
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"
-
May 13th, 2009, 06:28 PM
#5
Re: Inserting a new row in datagridview
Originally Posted by opashafiq
----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------
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.
-
August 24th, 2009, 05:58 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|