Click here to Skip to main content
15,611,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have datagridview which is Filled
with Database values.
I want to add rows in the DataGridView
on Add button click .

Can It is Possible.
Updated 17-Jan-19 4:56am

You can add new row to your DataTable (MyRecordset5):

dim MyNewRow as DataRow
with MyNewRow
end with

The datagridview will update automatically.
Share this answer
If the datagridview is not bound some datasource then you can add blank rows by

If your datagridview is bound, then adding a new item in datasource will automatically add a blank row in datagrid view.
Share this answer
Karwa_Vivek 27-Jun-12 7:33am    
Can YOu please Explain a bit How Can I Do that.
How Can I Add new Item In Datasource.
I am using a Stored procedure To Fetch Data And To Fill the Datagridview.
Kiran Susarla 27-Jun-12 7:48am    
Can you please post how you have done till now.
Karwa_Vivek 27-Jun-12 8:09am    
Have DOne like This
Private Sub CreateDataGridView(ByVal ChallanID As Integer)
Dim sSQL As String = ""
Dim MyRecordset5 As Data.DataTable = Nothing
MyRecordset5 = New Data.DataTable

sSQL = "SPIS_GetTransDtl " & ChallanID
MyConnection.ExecuteSQL(sSQL, MyRecordset5)
If MyRecordset5.Rows.Count > 0 Then
rowCnt = MyRecordset5.Rows.Count
'lblTotal.Text = MyRecordset5.Rows.Count.ToString & " Items"
dgv.AutoGenerateColumns = False
dgv.DataSource = MyRecordset5
dgv.Columns("SlNo").DataPropertyName = "Slno"
dgv.Columns("Item").DataPropertyName = "Name"
dgv.Columns("ItemId").DataPropertyName = "ItemID"
dgv.Columns("ItemCode").DataPropertyName = "ItemCode"
dgv.Columns("Quantity").DataPropertyName = "Qty"
dgv.Columns("Description").DataPropertyName = "Description"

End If

End Sub
try like this
   Dim f As Integer
          f = datagridview1.Rows.Count
         ' Add Values to the new row ...
         Dim n As Integer = datagridview1.Rows.Add()
         datagridview1.Rows.Item(n).Cells(0).Value = f + 1
         datagridview1.Rows.Item(n).Cells(1).Value = txtJob.Text
         datagridview1.Rows.Item(n).Cells(2).Value = cboBatch.Text
         datagridview1.Rows.Item(n).Cells(3).Value = ""
MessageBox.Show("New record added below. Please insert details.")
Share this answer
Karwa_Vivek 27-Jun-12 7:37am    
I have tried This .. But It Throws error that Adding Rows Programmaticaly is not valid in bound DatagridView.
I have Filled the Datagridview With Database Values using Stored Procedure.

if your want to add a Row directly from datadridviewObject:

1. take one existing ExistRow in Datagridview
2. add this ExistRow by (DataBoundItem.GetType.ToString)to your Bindingsource Object

This code add a new empty Row:

ObjectBindingSource.Add(Type.GetType(ExistRow.DataBoundItem.GetType.ToString()).GetConstructor(New System.Type() {}).Invoke(New Object() {}))

Datagridview will update automatically

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900