Hi!
everyone I hope u help me!
I have a datagridview how will i insert a record through datagrid.... I did't have dataset, staightly stored the database......

Dani AI

Generated

bound the grid at design time and can read rows but not persist edits. pointed toward per-row saves; that is one valid pattern. Two reliable, common approaches are described below (WinForms DataGridView bound to a DataTable, and ASP.NET GridView/SqlDataSource or manual commands). Each approach requires parameterized commands and a key column so the data layer can track inserts/updates/deletes.

For a WinForms DataGridView bound to a DataTable use a SqlDataAdapter (or OleDbDataAdapter), a BindingSource, and let the adapter perform the DB update. Example pattern (VB.NET):

' one-time setup
Dim connString As String = "Data Source=...;Initial Catalog=...;Integrated Security=True"
Dim conn As New SqlClient.SqlConnection(connString)
Dim adapter As New SqlClient.SqlDataAdapter("SELECT Id, Name FROM MyTable", conn)
Dim builder As New SqlClient.SqlCommandBuilder(adapter)   ' generates Insert/Update/Delete if table has a key
Dim table As New DataTable()
adapter.Fill(table)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1

' to save changes
BindingSource1.EndEdit()
adapter.Update(table)

For ASP.NET GridView use a declarative SqlDataSource with InsertCommand/UpdateCommand, or handle RowUpdating and execute a parameterized SqlCommand in code-behind. Example snippet for an update in RowUpdating (VB.NET):

Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
    Dim id = Convert.ToInt32(GridView1.DataKeys(e.RowIndex).Value)
    Dim txt = CType(GridView1.Rows(e.RowIndex).FindControl("txtName"), TextBox).Text
    Using cn As New SqlClient.SqlConnection(connString)
        Using cmd As New SqlClient.SqlCommand("UPDATE MyTable SET Name=@Name WHERE Id=@Id", cn)
            cmd.Parameters.AddWithValue("@Name", txt)
            cmd.Parameters.AddWithValue("@Id", id)
            cn.Open()
            cmd.ExecuteNonQuery()
        End Using
    End Using
End Sub

Important notes: ensure the DataTable has a primary key (SqlCommandBuilder needs it), always use parameterized commands, call EndEdit() on the BindingSource before Update(), and avoid calling AcceptChanges() before Update() (it clears change tracking). Concurrency and validation should be handled as needed.

Recommended Answers

All 2 Replies

If you are not binding the datagrid with any DataSource then one way of doing is by adding a button type column in each row of the grid and when you click on it the current row will be saved. You can use the DataAdaptor to save the record. Let me know if you need any example.

Hi
I binding the data design time...and binding the datagrid one datasource....but i don,t know how to store the database through datagrid but i retrive the data in the datagridview plz Help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.