• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2908
  • Last Modified:

update and delete a row from a datagrid

I need to update and delete a particular row from a datagrid in ASP.NET and the database is MS Access.
I have an update and delete button for each row. When I click on update the data should be open for edit and after editing I click on save the data should be saved.
When I click delete the row should be delted from datagrid and database
0
dhar116
Asked:
dhar116
1 Solution
 
tovvenkiCommented:
Hi,
it is very easy use button columns for Edit and Delete and use the OnEditCommand and OnDeleteCommand procedures to edit and delete the data.

<asp:datagrid id="DataGrid1" runat="server" DataKeyField="Regionid" OnDeleteCommand="DataGrid1_Delete"
      OnEditCommand="DataGrid1_Edit" OnCancelCommand="DataGrid1_Cancel">
<Columns>
     <asp:ButtonColumn Text="Delete" CommandName="Delete" />
</Columns>
<Columns>
     <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</asp:datagrid>

public void DataGrid1_Cancel(Object sender, DataGridCommandEventArgs e)
 {
      DataGrid1.EditItemIndex = -1;
      BindGrid();
 }
 
public void DataGrid1_Edit(Object sender, DataGridCommandEventArgs e)
 {
      DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;                
      BindGrid();
 }
 
public void DataGrid1_Update(Object sender, DataGridCommandEventArgs e)
 {
      string columnvalue =((TextBox)e.Item.Cells[3].Controls[0] ).Text ;
 
      //get the column values as above construct you update query here and update the database
}

public void DataGrid1_Delete(Object sender, DataGridCommandEventArgs e)
{
     int orderid=(int) DataGrid1.DataKeys[(int)e.Item.ItemIndex];;
     //get the id of the row to delete as above
     //construct your delete query and delete the row from the database.
     BindGrid();
 
}
 
Hope that this helps you

regards,
Venki
 



 
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now