How can i add, delete, update using datagrid?currently, im using Sql Server as the database.

Dani AI

Generated

is trying to insert a master row from textboxes and multiple detail rows coming from a DataGrid in a Windows Forms app. correctly separated web vs. WinForms earlier, and ’s snippet looks like a WebForms pattern (FindControl on template cells) — that approach does not map cleanly to WinForms. Two reliable WinForms patterns work well here: DataSet/DataAdapter binding with a DataRelation and BindingSource, or an explicit ADO.NET transaction that inserts the master, reads its identity, then inserts the detail rows.

If you use a DataSet/DataAdapter:

  • Fill master and detail DataTables and add a DataRelation.
  • Bind the master controls to a BindingSource and bind the detail grid to the related BindingSource.
  • When adding, create a new master DataRow (do not call AcceptChanges), call the master DataAdapter.Update() with an InsertCommand that returns the new identity (use SELECT SCOPE_IDENTITY() and set UpdatedRowSource so the ID is written back to the DataTable), then add detail rows and call the detail DataAdapter.Update(). See the Windows Forms master/details walkthrough for examples: .

If you prefer explicit SQL (often simpler to reason about):

  • Open a SqlConnection, BeginTransaction, insert the master row, get the new key with SCOPE_IDENTITY(), then loop the DataGrid rows and insert detail rows using parameterized commands, and commit. Example skeleton (VB.NET):
Using cn As New SqlConnection(connString)
  cn.Open()
  Dim tx = cn.BeginTransaction()
  Try
    Dim cmdMaster As New SqlCommand("INSERT INTO Orders(...) VALUES(...); SELECT SCOPE_IDENTITY();", cn, tx)
    ' add parameters...
    Dim masterId = Convert.ToInt32(cmdMaster.ExecuteScalar())
    ' loop grid rows, insert details with same tx
    tx.Commit()
  Catch ex As Exception
    tx.Rollback()
    Throw
  End Try
End Using

Troubleshooting tips: call BindingSource.EndEdit() or DataGridView.EndEdit() before reading values; do not call AcceptChanges before Update; ensure primary key/foreign key types match; use parameterized commands to avoid SQL injection; use SCOPE_IDENTITY() (not @@IDENTITY) when reading the inserted ID (SCOPE_IDENTITY documentation).

Recommended Answers

All 6 Replies

That highly depends on whether you meant Windows Forms or Web forms.
If you're using .net 2.0, use GridView, not DataGrid.

it is windows form. i know, the binding navigator can be used to add, delete, but i had to used button. so i didnt know how to add or delete without using the binding navigator. can anyone help me?

here is a sample code: modify according to ur need

this is for update task

TextBox tb1=new TextBox();
TextBox tb2=new TextBox();
TextBox tb3=new TextBox();
tb1=(TextBox)e.Item.Cells[2].Controls[0];
tb2=(TextBox)e.Item.Cells[3].Controls[0];
tb3=(TextBox)e.Item.Cells[6].Controls[0];
DropDownList ddl1=(DropDownList)e.Item.Cells[4].FindControl("dropdowndept");
string deptid=ddl1.SelectedItem.Value;
DropDownList ddl2=(DropDownList)e.Item.Cells[5].FindControl("dropdowndesgi");
string desgiid=ddl2.SelectedItem.Value;
int empid=Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);

i think rest of the operations are easy.
if u r using indexing then u may have a problem in deleting a record when there is a single record in datagrid or in page.these 2 are diferent u hav this prob then ask me.

thanks for ur reply, but for the update task, i already know the code. right now, i m having problem for adding data because it involves master detail relationship and i have to add the data from text box and also datagrid.

just access the datagrid control value by findcontrol method by passing the id of the control in that.i think this is a sql problem rather than a grid problem.can u tell me in detail abt ur problem.

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.