Hi . I have an employee table with fields Employee Id, Employee Name, Address,Salary , Department ID and department table with fields DeptID,DeptName.
I have added a data grid control with the columns showing Empname , address , salary,DeptName and Edit and Delete columns .
I have inserted image using template columns in the Edit and Delete column .
By clicking on the image in delete column , it should delete that particular row from the datagrid and from database and on refreshing the grid the changes should be shown in the datagrid .
Pleas help .

Hi. m able to delete rows from datagrid now . Now d second part is that once i click on edit column , it should redirect to a another website that i have created for adding and updating employee details . i have written d foll code . Please let me know what changes to be made :
In d HTML part : (I am sending the code for the edit column only)

 <asp:TemplateColumn HeaderText="Edit">
                    <ItemTemplate>
                        <asp:Image runat="server" Width="50" Height="40" ImageUrl="~/Images/images.jpg" />
                         <%--<asp:HyperLinkField DataNavigateUrlFields="Name,Address, Salary" DataNavigateUrlFormatString="AddEditEmployee.aspx?name={0}@address={1}@salary={2}" Text="Edit"  runat= "server" /> 
                         </asp:HyperLinkField > --%> 
               <asp:HyperLink ID="empinfo" Text="Edit " runat="server"  NavigateUrl='<%# "Default.aspx? employee=" + Eval("EmpiD") + "&EmpName=" + Server.UrlEncode(Eval("EmpName").ToString())%>' /> 
                    </ItemTemplate>
               </asp:TemplateColumn>

                <asp:ButtonColumn CommandName="Edit" Text="Edit"  ></asp:ButtonColumn>

And in code behind file :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security; 
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data;




    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection(); 



            DataGrid1.DeleteCommand+=new DataGridCommandEventHandler(DataGrid1_DeleteCommand);


            if (!Page.IsPostBack)
            {



            FillDataGrid();

        }
    }


    public void FillDataGrid()
    {

        String strConnString = ConfigurationManager.ConnectionStrings["Company"].ConnectionString;

        SqlConnection con = new SqlConnection(strConnString);

        SqlCommand cmd = new SqlCommand();

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.CommandText = "proc_Getallempdetails";

        cmd.Connection = con;

        try
        {

            con.Open();



            DataGrid1.DataSource = cmd.ExecuteReader();

            DataGrid1.DataBind();

        }

        catch (Exception ex)
        {

            throw ex;

        }

        finally
        {

            con.Close();

            con.Dispose();

        }
    }


    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {
      string dbcon = ConfigurationManager.ConnectionStrings["Company"].ConnectionString;
      SqlConnection con = new SqlConnection(dbcon);
      SqlCommand cmd = new SqlCommand("Delete from Employee where Empid=" + DataGrid1.DataKeys[e.Item.ItemIndex].ToString(), con);
       con.Open();
        int rows = cmd.ExecuteNonQuery();
        if (rows > 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Deleted", "<script>alert('Deleted Successfully')</script>");
            con.Close();
            FillDataGrid();
        }

    }

    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = e.Item.ItemIndex;
        FillDataGrid();
    }

     protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        TextBox name = (TextBox)DataGrid1.Items[e.Item.ItemIndex].Cells[1].FindControl("txtname_e");
        TextBox address= (TextBox)DataGrid1.Items[e.Item.ItemIndex].Cells[2].FindControl("txtadd_e");
        TextBox salary = (TextBox)DataGrid1.Items[e.Item.ItemIndex].Cells[1].FindControl("textbox3");
         string dbcon = ConfigurationManager.ConnectionStrings["Company"].ConnectionString;
        SqlConnection con = new SqlConnection(dbcon);
        SqlCommand cmd = new SqlCommand("Update Employee set EmpName='" + name.Text + "',Address='" + address.Text + "' where Empid=" + DataGrid1.DataKeys[e.Item.ItemIndex].ToString(), con);
        con.Open();
        int rows = cmd.ExecuteNonQuery();
        if (rows > 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Updated", "<script>alert('Updated Successfully')</script>");
            con.Close();
            DataGrid1.EditItemIndex = -1;
            FillDataGrid();
        }
    }

} 

Code for Addeditemployee.aspx :
In the HTML part :

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

<p>
          
        <table border="1">


    <tr>
                    <td>
                        Name:
                    </td>
                    <td class="style1">
                    <form runat ="server">
                        <asp:TextBox ID="txtname_e" runat="server" Width="167px"></asp:TextBox>
                        </form> 
                    </td>
                </tr>
                <tr>
                    <td>
                        Address:
                    </td>
                    <td class="style1"> 
                    <form>
                        <asp:TextBox ID="txtadd_e" runat="server" TextMode="MultiLine" Width="176px"></asp:TextBox>
                        </form>
                        <br />
                    </td>
                </tr>
                <tr>
                    <td class="style2" >
                    Salary :
                    </td>
                     <td class="style3"> 

                       <asp:TextBox ID="textbox3" runat="server"

Width="176px"></asp:TextBox>

                </td>  
            </tr>
            <tr>


            <td colspan= "1" >
                Department name :
            </td>

           <td> 
                <asp:DropDownList runat="server" ID="DropDownList1" Height="31px" 
                    Width="179px" >
                   <asp:ListItem> </asp:ListItem>

                </asp:DropDownList>



                          <br />
                    <br />
                    <br />
                      
                    </td>
                    </tr> 
                    <tr>
                    <td colspan= 2 align= center> 
                    <asp:Button ID="btnadd" runat="server" CommandName="AddNew" Text="Submit" 
                            onclick="btnadd_Click" ></asp:Button> 
                             
                    <asp:Button ID="Button2" runat="server" CommandName="Cancel" Text="Cancel" 
                            onclick="Button2_Click" ></asp:Button> 
                        
                     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add New Record" ></asp:Button>
                   </td> 

                   </tr>  

        </table>
         </p>
    <p>
 <br />
    </p>
    <p>
    </p>
    <p>
    </p>
    <p>
    </p>
    <p>
    </p>
    <p>
    </p>
</asp:Content>

And in the code behind file :

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
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //string strName = Request.QueryString["name"];
        //string strAddress = Request.QueryString["address"];
        //string strSalary = Request.QueryString["salary"];
        //txtname_e.Text = strName;
        //txtadd_e.Text = strAddress;
        //textbox3.Text = strSalary;
    }

If you just need to redirect, just use Response.Redirect, like:

protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {
        DataGrid1.EditItemIndex = e.Item.ItemIndex;
        Response.Redirect("MyPage.aspx?index=" + e.Item.ItemIndex);
    }
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.