Link to home
Start Free TrialLog in
Avatar of lta006
lta006Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Using GridView to delete records.

Hi,

I'm confused! I'm using Visual Studio 2008 and I've just started to use a GridView in an ASP.Net page to view and deleting data in a database. Am I doing this correctly? _ My example works, but I don't think I should have to write code to delete the record. When reading the Microsoft documentation I got the distinct impression that the RowDeleting event was there purely as my last chance to cancel the deletion of the record, and I didn't need to write the code to delete it.

I've built a table adapter using the wizards and it seems to have successfully generated the insert, delete and update commands because I can see them in the XSD file. The RowDeleting event is being fired but no actual data is being deleted from the database. I can write code to manually call the tableadapter's Delete method to delete the record but shouldn't the GridView be doing that for me?

My ASPX page contains :

<asp:GridView ID="gvData" runat="server" AutoGenerateDeleteButton="true" OnRowDeleting="gvData_RowDeleting" />
<asp:Label ID="lblMessage" runat="server" />

Open in new window


And my c# code is:

protected void BindData()
{
    MyTableAdapter taData = new MyTableAdapter();
    gvData.DataSource = taData.Getdata();
    gvData.DataKeyNames = new string[] {"ID"} ;
    gvData.DataBind();
    taData>Dispose();
}

protected void Page_Load(object sender, EventArgs e)
{
    BindData();
    lblMessage.Text = "";
}

protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    lblMessage.Text += string.Format("<br/>RowDeleting, e.RowIndex = {0}", e.RowIndex);
    int ID = int.Parse(gvData.Rows[e.RowIndex].Cells[1].Text);  // The ID field is in this cell
    lblMessage.Text += string.Format("<br/>Delete ID = {0}", ID);
    
    // The following two lines delete the record successfully, but why do I need them? Shouldn't the gridview delete the record for me? 
    MyTableAdapter taData = new MyTableAdapter();
    taData.Delete(ID);

    BindData();
}

Open in new window


I should mention that the table that I'm working with has a primary key that's an integer called ID
Avatar of tan_dev
tan_dev
Flag of United Kingdom of Great Britain and Northern Ireland image

delete command can be executed few other event like selectindexchanged or rowcommand or just use it as a button_click event. there are few ways to achieve this.

but deletion is going to be a manual process of telling the grid what to delete. you can automate the delete by using a sqldatasource but thats not a good way to work with a gridview.
Avatar of lta006

ASKER

Thanks,

So in the RowDeleting method I need to check whether the user really meant to click delete and then if they did mean it, call the Delete method on the TableAdapter, otherwise do nothing.

Doesn't quite tie in with my reading f the documentation, but it's good to know I'm not going mad!

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of tan_dev
tan_dev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kumaraswamy R
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.