views:

20705

answers:

7

Hi

How to add new record to datagridview control in VB.net, I don't use Dataset or database biding at all, all I have is small form with 3 entries, when the user click OK, that will be added to the datagridview control as new row.

Thanks

+1  A: 

I think you should build a dataset/datatable in code and bind the grid to that.

Galwegian
A: 

The function you're looking for is 'Insert'. It takes as its parameters the index you want to insert at, and an array of values to use for the new row values. Typical usage might include:

myDataGridView.Rows.Insert(4,new object[]{value1,value2,value3});

or something to that effect.

GWLlosa
+2  A: 

If you want to add the row to the end of the grid use the Add() method of the Rows collection...

DataGridView1.Rows.Add(New String(){Value1, Value2, Value3})

If you want to insert the row at a partiular position use the Insert() method of the Rows collection (as GWLlosa also said)...

DataGridView1.Rows.Insert(rowPosition, New String(){value1, value2, value3})

I know you mentioned you weren't doing databinding, but if you defined a strongly-typed dataset with a single datatable in your project, you could use that and get some nice strongly typed methods to do this stuff rather than rely on the grid methods...

DataSet1.DataTable.AddRow(1, "John Doe", true)
whatknott
A: 
        connection.Open()
        oledbAdapter = New OleDbDataAdapter(Sql, connection)
        oledbAdapter.Fill(ds)
        DataGridView1.Data Source= ds.Tables(0)

http://vb.net-informations.com/dataadapter/dataadapter-datagridview-sqlserver.htm

bolt.

bolton
A: 

Really these answers are very benifically for me.

thanx a lot.

Gursewak Singh
A: 

Try this link

http://vb.net-informations.com/datagridview/vb.net_datagridview_add.htm

gever.

Gever
A: 

Thanx Friends!!! It was ver usefull

Guest