Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Edit,Delete, Cancel,Update,Insert Values in gridview using asp.net
Posted
Comments
Smithers-Jones 29-Jun-11 11:12am    
Yours isn't a question, not even a full sentence. Are you just too lazy to phrase a proper sentence or incapable?
Sergey Alexandrovich Kryukov 30-Jun-11 18:04pm    
Yes.
Sergey Alexandrovich Kryukov 30-Jun-11 18:04pm    
Not a question!
--SA

Search[^] on CP.
 
Share this answer
 
 
Share this answer
 
//-----------the below the design(.aspx)
//-----------------------------------------------------------
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="eg4.aspx.cs" Inherits="eg4" %>

<!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 id="Head1" runat="server">
<title>Billing</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" DataKeyNames="sno,MedincieName,Rs,Quatity,Total" runat="server"
AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowcancelingedit="gvDetails_RowCancelingEdit"
onrowdeleting="gvDetails_RowDeleting" onrowediting="gvDetails_RowEditing"
onrowupdating="gvDetails_RowUpdating"
onrowcommand="gvDetails_RowCommand"


       >
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="S.No">
<EditItemTemplate>
<asp:Label ID="lbleditsno" runat="server" Text='<%#Eval("sno") %>'/>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="lblitemSno" runat="server" Text='<%#Eval("sno") %>'/>
</ItemTemplate>

<FooterTemplate>
<asp:TextBox ID="txtftrsno" runat="server"/>
<asp:RequiredFieldValidator ID="rfvsno" runat="server" ControlToValidate="txtftrsno" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="MedicinceName">
<EditItemTemplate>
<asp:TextBox ID="txtmed" runat="server" Text='<%#Eval("MedincieName") %>'/>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="lblmed" runat="server" Text='<%#Eval("MedincieName") %>'/>
</ItemTemplate>

<FooterTemplate>
<asp:TextBox ID="txtftrmed" runat="server"/>
<asp:RequiredFieldValidator ID="rfvmed" runat="server" ControlToValidate="txtftrmed" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Rs/Unit">
<EditItemTemplate>
<asp:TextBox ID="txtrs" runat="server" Text='<%#Eval("Rs") %>'/>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="lblrs" runat="server" Text='<%#Eval("Rs") %>'/>
</ItemTemplate>

<FooterTemplate>
<asp:TextBox ID="txtftrrs" runat="server"/>
<asp:RequiredFieldValidator ID="rfvrs" runat="server" ControlToValidate="txtftrrs" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Quatity">
<EditItemTemplate>
<asp:TextBox ID="txtqut" runat="server" Text='<%#Eval("Quatity") %>'/>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="lblqut" runat="server" Text='<%#Eval("Quatity") %>'/>
</ItemTemplate>

<FooterTemplate>
<asp:TextBox ID="txtftrqut" runat="server"/>
<asp:RequiredFieldValidator ID="rfvqut" runat="server" ControlToValidate="txtftrqut" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Total">
<EditItemTemplate>
<asp:TextBox ID="txttot" runat="server" Text='<%#Eval("Total") %>'/>
</EditItemTemplate>

<ItemTemplate>
<asp:Label ID="lbltot" runat="server" Text='<%#Eval("Total") %>'/>
</ItemTemplate>

<FooterTemplate>
<asp:TextBox ID="txtftrtot" runat="server"/>
<asp:RequiredFieldValidator ID="rfvtot" runat="server" ControlToValidate="txtftrtot" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>
</div>
<div>
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</form>
</body>
</html>

//------------------------------------


//-----------the below the code(.aspx.cs)
//------------------------------------

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class eg4 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=eg;Data Source=UDAY");

     GridViewRow gvrow;
     DataTable dt = new DataTable();
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            medDetails();
        }
         
    }
  
    protected void medDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from med1", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvDetails.DataSource = ds;
            gvDetails.DataBind();

        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvDetails.DataSource = ds;
            gvDetails.DataBind();
            int columncount = gvDetails.Rows[0].Cells.Count;
            gvDetails.Rows[0].Cells.Clear();
            gvDetails.Rows[0].Cells.Add(new TableCell());
            gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvDetails.Rows[0].Cells[0].Text = "No Records Found";

        }
    }
   
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        medDetails();
    }
    
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //int sno = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        string medinciename = gvDetails.DataKeys[e.RowIndex].Values["MedincieName"].ToString();
        TextBox txtmed = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtmed");
        TextBox txtrs = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtrs");
        TextBox txtqut = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtqut");
        TextBox txttot = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txttot");
        con.Open();
        SqlCommand cmd = new SqlCommand("update med1 set MedincieName='" +txtmed.Text + "',Rs='" + txtrs.Text + "',Quatity='"+txtqut.Text+"' where MedincieName='" + medinciename + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = medinciename + " Details Updated successfully";
        gvDetails.EditIndex = -1;
       medDetails();
        
    }
   
    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
       medDetails();
    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       //  int sno = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["sno"].ToString());
        string medinciename = gvDetails.DataKeys[e.RowIndex].Values["MedincieName"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from med1 where MedincieName='" + medinciename+"'", con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            medDetails();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = medinciename + " details deleted successfully";
        }
    }
   
    protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtmed = (TextBox)gvDetails.FooterRow.FindControl("txtftrmed");
            TextBox txtrs = (TextBox)gvDetails.FooterRow.FindControl("txtftrrs");
            TextBox txtqut = (TextBox)gvDetails.FooterRow.FindControl("txtftrqut");



            
            con.Open();
            SqlCommand cmd =
            new SqlCommand("insert into med1(MedincieName,Rs,Quatity) values('" + txtmed.Text + "','" + txtrs.Text + "','" + txtqut.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                medDetails();
                lblresult.ForeColor = Color.Green;
                lblresult.Text = txtmed.Text + " Details inserted successfully";
            }
            else
            {
                lblresult.ForeColor = Color.Red;
                lblresult.Text = txtmed.Text + " Details not inserted";
            }
        }
    }

    protected void gvDetails_SelectedIndexChanged(object sender, GridViewRowEventArgs e)
    {
        //---------------------
        //{
        //    TextBox txtrs = (TextBox)gvDetails.FooterRow.FindControl("txtftrrs");
        //    TextBox txtqut = (TextBox)gvDetails.FooterRow.FindControl("txtftrqut");
        //    TextBox txttot = (TextBox)gvDetails.FooterRow.FindControl("txtftrtot");

        //    txtrs.Text = gvDetails.SelectedRow.Cells[3].Text;
        //    txtqut.Text = gvDetails.SelectedRow.Cells[4].Text;
        //    txttot.Text = gvDetails.SelectedRow.Cells[5].Text;

        //    int id = Convert.ToInt32(e.Row.Cells[3].Text);
        //    int id2 = Convert.ToInt32(e.Row.Cells[4].Text);
        //    int id3 = Convert.ToInt32(e.Row.Cells[5].Text);


        //    id3 = id * id2;

        //    e.Row.Cells[e.Row.Cells.Count - 1].Text = id3.ToString();
        //}
        //---------------------  

    }
    }






//------------------------------------
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900