Using GridView to delete records.

lta006
lta006 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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.

Author

Commented:
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
Commented:
Yes thats quite simple to achieve. In your design view add following javascript:

<script type="text/javascript">
    function confirm_delete() {
        if (confirm("Are you sure you want to delete?") == true)
            return true;
        else
            return false;
    }
</script>

Now register this javascript to your delete link by putting the following code in RowCreated event:

 protected void gvContacts_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lnkButton = (LinkButton)e.Row.FindControl("lnkDelete");
                lnkButton.Attributes.Add("onclick", "return confirm_delete();");
            }
        }

This should do it for you. I'd avoid letting gridview auto access your database for various reasons.

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial