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
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 ¿
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] = "[email protected]";
tabel.Rows.Add(row);
Re: Inserting a new row in datagridview
Quote:
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"
Re: Inserting a new row in datagridview
Quote:
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.
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