Hey All

Im using 3 tier design to do my coding. Im having problems deleting a record selected in the grid. I have my method which does the loading of the datagridview in the businesslayer.

Can you please assist me on how to delete a record selected...

Please

Dani AI

Generated

described a 3-tier app where the business layer loads the grid, and suggested removing the row from the grid UI. Removing a row only in the control is not enough in a layered design. The usual, safe pattern is: read the selected row's primary key, call a business-layer delete method that delegates to the data layer (using a parameterized command), and then refresh the grid by reloading data from the business layer. This keeps database state and UI state in sync and keeps data logic centralized.

Recommended checklist:

  • Expose a stable primary key for each row (hidden column or GridView.DataKeyNames) (GridView.DataKeyNames).
  • Get the key from the selected row (for WinForms use the current row; see DataGridView.CurrentRow).
  • Call BusinessLayer.DeleteById(id) which calls a DAL method that executes a parameterized command (use SQL parameters, see SqlCommand.Parameters).
  • On success, rebind the grid by calling the existing load method from the business layer.

Minimal patterns (illustrative only):

/* WinForms button click */
int id = (int)myGrid.CurrentRow.Cells["Id"].Value;
if (ConfirmDelete()) {
    business.DeleteById(id);
    LoadGrid(); // rebind from business layer
}
/* ASP.NET GridView RowCommand */
int id = (int)GridView1.DataKeys[rowIndex].Value;
if (business.DeleteById(id)) BindGrid();

Troubleshooting notes: if the UI change does not persist, check that the DAL actually executes the DELETE and returns success (log SQL or use a profiler). For DataTable-based binding, call the adapter/tableAdapter Update to persist row deletions. Consider optimistic concurrency (rowversion/timestamp) and wrap DB operations in try/catch and transactions when needed.

Recommended Answers

All 2 Replies

I think its a simple task. First of all you have to obtain an index of row you want to delete, and use Rows.RemoveAt(row_index) method of dataGridView control to remove that row.

thanx..

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.