Friday, 12 July 2013

GridviewEditing

CODE IN ASPX SOURCE

     <asp:GridView ID="Grid1" AutoGenerateColumns="False" ShowFooter="True"
         DataKeyNames="ProductId"   runat="server"  AutoGenerateEditButton="True"
AutoGenerateDeleteButton="True"  onrowcancelingedit="Grid1_RowCancelingEdit" onrowdeleting="Grid1_RowDeleting" 
            onrowediting="Grid1_RowEditing" onrowupdating="Grid1_RowUpdating" 
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
        <Columns>
        <asp:BoundField DataField="ProductId" HeaderText="ProductId" ReadOnly="true" />
        <asp:TemplateField HeaderText="ProductName">
        <ItemTemplate><%#Eval("ProductName")%></ItemTemplate>
           <EditItemTemplate>
           <asp:TextBox ID="txtProductName" runat="server" 
           Text='<%#Eval("ProductName") %>' />
           </EditItemTemplate>
           <FooterTemplate>
           <asp:TextBox Id="txtNewproductName" runat="server"></asp:TextBox>
           </FooterTemplate>
           </asp:TemplateField>
           
           <asp:TemplateField HeaderText="ProductBrand">
        <ItemTemplate><%#Eval("ProductBrand")%></ItemTemplate>
           <EditItemTemplate>
           <asp:TextBox ID="txtProductBrand" runat="server" 
           Text='<%#Eval("ProductBrand") %>' />
           </EditItemTemplate>
           <FooterTemplate>
           <asp:TextBox Id="txtNewproductBrand" runat="server"></asp:TextBox>
           </FooterTemplate>
           </asp:TemplateField>
           <asp:TemplateField HeaderText="Price">
        <ItemTemplate><%#Eval("Price")%></ItemTemplate>
           <EditItemTemplate>
           <asp:TextBox ID="txtPrice" runat="server" 
           Text='<%#Eval("Price") %>' />
           </EditItemTemplate>
           <FooterTemplate>
           <asp:TextBox Id="txtNewprice" runat="server"></asp:TextBox>
           </FooterTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Insert">  
        
      </asp:TemplateField>
    

</Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
 </asp:GridView>




Code in aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using GridInsert_BusinessLayer;

public partial class _Default : System.Web.UI.Page
{
    LookUp objLookUp = new LookUp();
    DataSet ds = new DataSet();
    insertDetails ObjinsertDetails = new insertDetails();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }

    }
    public void DataBind()
    {
        ds = objLookUp.LoadMain();
        gdv.DataSource = ds;
        gdv.DataBind();
        Session["grid"] = gdv.DataSource;
    }

    protected void btnInsert(Object sender, EventArgs e)
    {
        TextBox txtPName = (TextBox)(gdv.FooterRow.FindControl("txtPNameFoot"));
        TextBox txtbrand = (TextBox)(gdv.FooterRow.FindControl("txtbrandFoot"));
        TextBox txtprice = (TextBox)(gdv.FooterRow.FindControl("txtPriceFoot"));

        ObjinsertDetails.PName = txtPName.Text;
        ObjinsertDetails.Brand = txtbrand.Text;
        ObjinsertDetails.Price = Convert.ToDouble(txtprice.Text);
        int i=objLookUp.Insert(ObjinsertDetails);
        DataBind();
        
    }

    protected void Row_Update(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtPName = (TextBox)(gdv.Rows[gdv.EditIndex].FindControl("txtPNameFoot"));
        TextBox txtbrand = (TextBox)(gdv.Rows[gdv.EditIndex].FindControl("txtbrandFoot"));
        TextBox txtPrice = (TextBox)(gdv.Rows[gdv.EditIndex].FindControl("txtPriceFoot"));

        ObjinsertDetails.PName = txtPName.Text;
        ObjinsertDetails.Brand = txtbrand.Text;
        ObjinsertDetails.Price = Convert.ToDouble(txtPrice.Text);
        int i = objLookUp.Updating(ObjinsertDetails);
        DataBind();
    }
    protected void Row_Edit(object sender, GridViewEditEventArgs e)
    {
        gdv.EditIndex = e.NewEditIndex;
        DataBind();

    }
    protected void Row_Delete(object sender, GridViewDeleteEventArgs e)
    {
        if (gdv.Rows.Count > 0)

        {
            int PId = int.Parse(gdv.DataKeys[e.RowIndex].Value.ToString());
            int ProductId = PId;
            int i = objLookUp.Delete(PId);
        }
    }
    protected void Row_Cancel(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        gdv.EditIndex = -1;
        DataBind();
        
    }
   
}


StoreProcedure Insert




ALTER PROCEDURE [dbo].[MainInsert]
@ProductName varchar(50),
@ProductBrand varchar(50),
@Price int 
AS
BEGIN
insert into tblMain(ProductName,ProductBrand,Price)values(@ProductName,@ProductBrand,@Price)
END

StoreProcedure update

ALTER PROCEDURE [dbo].[MainUpdate]
@PId int,
@ProductName varchar(50),
@ProductBrand varchar(50),
@Price int
AS
BEGIN
update  tblMain set ProductName=@ProductName,ProductBrand=@ProductBrand,@Price=@Price
where ProductId=@PId
END


StoreProcedure Delete


ALTER PROCEDURE [dbo].[MainDelete]
@PId int  
AS
BEGIN
delete tblMain where ProductId=@PId
END

No comments:

Post a Comment