Click here to Skip to main content
15,888,610 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.
Assist
Posted
Updated 17-Jan-19 4:56am

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

VB
dim MyNewRow as DataRow
MyNewRow=MyRecordset5.NewRow
with MyNewRow
   .Item(0)="NewItem"
   .Item(1)=1234
...
end with
MyRecordset5.rows.add(MyNewRow)
MyRecordset5.acceptchanges


The datagridview will update automatically.
 
Share this answer
 
If the datagridview is not bound some datasource then you can add blank rows by
C#
datagridview1.Rows.Add()

method.
If your datagridview is bound, then adding a new item in datasource will automatically add a blank row in datagrid view.
 
Share this answer
 
Comments
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.
Thanks
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
VB
   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
 
Comments
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.
hello,

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

enjoy!
 
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