-
December 28th, 2006, 01:42 AM
#1
DataGridView Add Row
Hi, I have DataGridView and I have a Add New Row Button and a Delete Row Button. What is the code to add a new row in the datagridview? Also the code to delete a row from the datagridview?
Note: I do not want to enable the AllowUserToAddRows and AllowUserToDeleteRows properties of the datagridview because I want to do it programatically.
Thanks!
-
December 28th, 2006, 11:39 PM
#2
Re: DataGridView Add Row
Is your grid bound to a data source? If so then you should be adding and deleting rows in that data source, not the grid. If the grid is unbound then you call the Add and Remove methods of the grid's Rows collection.
-
December 29th, 2006, 04:02 PM
#3
Re: DataGridView Add Row
Adding a row to datagridview with textBox columns
Code:
Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = "anis"
dgvRow.Cells.Add(dgvCell)
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = "khan"
dgvRow.Cells.Add(dgvCell)
DataGridView1.Rows.Add(dgvRow)
-
December 29th, 2006, 04:09 PM
#4
Re: DataGridView Add Row
Remove and RemoveAt methods of datagridview can be used to remove a row
Code:
DataGridView1.Rows.RemoveAt(index)
Adding a row to the dataTable if datagridview is bound to it
Code:
Dim row As DataRow = dt.NewRow
row("Col1") = "anis"
row("Col2") = "khan"
dt.Rows.Add(row)
Similarly to remove row from datatable use remove and removeAt
Code:
dt.Rows.RemoveAt(index)
-
August 19th, 2009, 02:28 AM
#5
Re: DataGridView Add Row
i tried ur code. it is working. but when i add 4th column in the same row then it gives me an error. pl help me.
-
January 26th, 2010, 04:17 PM
#6
-
March 10th, 2010, 11:20 AM
#7
Re: DataGridView Add Row
I tried that code and following error message came up: "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound."
I use VB 2009 express am i doing something wrong?
I am a complete amateur, if you did not understood that yet.
-
March 10th, 2010, 12:20 PM
#8
Re: DataGridView Add Row
if you have a databound grid, you should add a row to datasource to see an extra row in the grid.
If you want a different kind of row (say a totals row) you should look for FooterRow...
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
May 29th, 2010, 04:05 PM
#9
Re: DataGridView Add Row
Dear aniskhan
when you talk about datagridview is bounded , I try your code :
Dim row As DataRow = dt.NewRow
row("Col1") = "anis"
row("Col2") = "khan"
dt.Rows.Add(row)
But it does not work , can you explain what you mean by dt
actully it's give mistake in " dt.NewRow " & " dt.Rows.Add(row) "
-
May 29th, 2010, 10:10 PM
#10
Re: DataGridView Add Row
You realize the post you are asking about is 4 years old. You may not get a response from the poster.
However if you look at the code it is pretty clear that dt is the datatable that the grid is bound to.
Always use [code][/code] tags when posting code.
-
August 25th, 2010, 03:19 AM
#11
Re: DataGridView Add Row
yo can find some useful datagridvire help from the following url
http://csharp.net-informations.com/d...w-tutorial.htm
gever.
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
|