1 Replies - 3582 Views - Last Post: 17 August 2011 - 06:56 AM

#1 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

c# asp.net DeleteCommand for datagrid with database

Posted 17 August 2011 - 12:23 AM

I am working on a minor database problem. I have createda simple database and populated it with some info. I now want to be able to edit the database from a DataGrid. So far I have the data populating it and have the ability to Update the rows in the datagrid.

However I would like the ability to deleted a row. I cannot get that to work though. Below is my code. What exactly do I need to do to remove a row from a datagrid? I am looking at
DeleteCommand=""
but have no idea what to try. I have tested several things and referenced
http://msdn.microsof...287(VS.71).aspx
for helpbut am not getting anywhere.

Advice would be awesome.

Thanks

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Pokedex</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Welcome to my Pokedex
        <asp:GridView ID="GridView1" AllowSorting="True" AllowPaging="True" Runat="server"
        DataSourceID="SqlDataSource1" AutoGenerateEditButton="True"
        AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:BoundField DataField="PokemonName" HeaderText="PokemonName" 
                    SortExpression="PokemonName" />
                <asp:BoundField DataField="PokemonType" HeaderText="PokemonType" 
                    SortExpression="PokemonType" />
                    <asp:BoundField DataField="HP" HeaderText="HP" 
                    SortExpression="HP" />
                <asp:BoundField DataField="Weakness" HeaderText="Weakness" 
                    SortExpression="Weakness" />
                <asp:BoundField DataField="Strength" HeaderText="Strength" 
                    SortExpression="Strength" />
                <asp:CommandField HeaderText="Delete Pokemon" ShowDeleteButton="True" 
                    ShowHeader="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:pokemondatabaseConnectionString %>" 
            SelectCommand="SELECT * FROM [PokemonData]"
           [b]DeleteCommand=""[/b]
             UpdateCommand="Update PokemonData SET Weakness=@Weakness, Strength=@Strength, HP=@HP, PokemonType=@PokemonType WHERE PokemonName=@PokemonName"
            ></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
{
 
   }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }

    private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        int rowToDelete = e.Item.ItemIndex;
        // Add code to delete row from data source.
        GridView1.DataBind();
    }
} 



Is This A Good Question/Topic? 0
  • +

Replies To: c# asp.net DeleteCommand for datagrid with database

#2 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: c# asp.net DeleteCommand for datagrid with database

Posted 17 August 2011 - 06:56 AM

I got it. Figured I would post solution for anybody who may need later on. Sometimes it just needs a couple hours sleep

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {    
    // Cancel the delete operation if the checkbox is not checked.
    if (! CheckBox1.Checked) {
        e.Cancel = true;
        Label1.Text = "The command was cancelled because the CheckBox was not checked.";
    }
 }

private void OnRecordDeleted(object source, SqlDataSourceStatusEventArgs e) {
    
    Label1.Text = e.AffectedRows + " row(s) were deleted";
}

void DeleteRowButton_Click(Object sender, EventArgs e)
{
    // Programmatically delete the selected record.
    GridView1.DeleteRow(GridView1.SelectedIndex);
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Pokedex</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Welcome to my Pokedex<br /><br />

    <asp:CheckBox 
         id="CheckBox1" 
         runat="server"
         autopostback="true"
         text="Check To Delete Data" />
        <br />
        <br />

       
        <asp:GridView ID="GridView1" AllowSorting="True" AllowPaging="True" Runat="server"
        DataSourceID="SqlDataSource1" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True"
        AutoGenerateColumns="False" onrowcancelingedit="GridView1_RowCancelingEdit" selectedindex="0" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" datakeynames="PokemonName" >
            <Columns>
                <asp:BoundField DataField="PokemonName" HeaderText="PokemonName" 
                    SortExpression="PokemonName" />
                <asp:BoundField DataField="PokemonType" HeaderText="PokemonType" 
                    SortExpression="PokemonType" />
                    <asp:BoundField DataField="HP" HeaderText="HP" 
                    SortExpression="HP" />
                <asp:BoundField DataField="Weakness" HeaderText="Weakness" 
                    SortExpression="Weakness" />
                <asp:BoundField DataField="Strength" HeaderText="Strength" 
                    SortExpression="Strength" />
                
            </Columns>
        </asp:GridView>
        <asp:Label
            id="Label1"
            runat="server">
        </asp:Label>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:pokemondatabaseConnectionString %>" 
            SelectCommand="SELECT * FROM [PokemonData]"
            
           DeleteCommand="DELETE FROM PokemonData WHERE PokemonName = @PokemonName"
           OnDeleting="OnRecordDeleting"
            OnDeleted="OnRecordDeleted"
             
            UpdateCommand="Update PokemonData SET Weakness=@Weakness, Strength=@Strength, HP=@HP, PokemonType=@PokemonType WHERE PokemonName=@PokemonName"
            ></asp:SqlDataSource>
         </div>
    </form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1