Tuesday, 15 October 2013

insert,update,delete from gridview

   <table width="100%">
                <tr>
                    <td>
                        <asp:GridView ID="gdvFooter" runat="server" AutoGenerateColumns="false" EmptyDataText="no data..."
                            DataKeyNames="pid" OnRowCommand="gdvFooter_RowCommand" OnRowCancelingEdit="gdvFooter_RowCancelingEdit"
                            OnRowDeleting="gdvFooter_RowDeleting" OnRowEditing="gdvFooter_RowEditing" OnRowUpdating="gdvFooter_RowUpdating" OnRowDataBound="gdvFooter_RowDataBound">
                            <Columns>
                                <asp:BoundField HeaderText="S.No." DataField="Sno" />
                                <asp:TemplateField HeaderText="Product No.">
                                    <ItemTemplate>
                                        <asp:Label ID="lbpnumber" runat="server" Text='<%#bind("pnumber")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtpnumberUP" runat="server" Text='<%#bind("pnumber")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtpnumber" runat="server"></asp:TextBox>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Product Name">
                                    <ItemTemplate>
                                        <asp:Label ID="lbpName" runat="server" Text='<%#bind("pname")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtpnameUP" runat="server" Text='<%#bind("pname")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtpname" runat="server"></asp:TextBox>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Product Price">
                                    <ItemTemplate>
                                        <asp:Label ID="lbpprice" runat="server" Text='<%#bind("pprice")%>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtppriceUP" runat="server" Text='<%#bind("pprice")%>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:TextBox ID="txtpprice" runat="server"></asp:TextBox>
                                    </FooterTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Sum">
                                    <ItemTemplate>
                                        <asp:Label ID="blbSum" runat="server"></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnbtnEdit" runat="server" CommandName="Edit" Text="Edit">
                                        </asp:LinkButton>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="UpDate">
                                        </asp:LinkButton>
                                        <asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete">
                                        </asp:LinkButton>
                                        <asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel">
                                        </asp:LinkButton>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:Button ID="btnAdd" runat="server" Text="Insert" CommandName="Insert" />
                                    </FooterTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        <asp:Button ID="btnAddNewRow" runat="server" Text="Add" OnClick="btnAddNewRow_Click" />
                        <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
                    </td>
                </tr>
            </table>
-------------------------------------------------------------------------------------------------------------------
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.IO;
using System.Data.SqlClient;


public partial class UseFooter : System.Web.UI.Page
{
    SqlDataAdapter da;
    SqlDataReader dr;
    DataSet ds;
    SqlCommand cmd;
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(@"data source=.\sqlexpress; initial catalog=AdminUse; integrated security=SSPI;");
        if (!IsPostBack == true)
        {
            loadGrid();
        }
    }
    private void loadGrid()
    {
        string sql = "select row_number() over(order by pname) as Sno,* from product order by pname";

        da = new SqlDataAdapter(sql, con);
        ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            gdvFooter.DataSource = ds.Tables[0];
            gdvFooter.DataBind();
        }
        else
        {

            gdvFooter.ShowFooter = true;
            gdvFooter.DataBind();

        }

    }
    protected void gdvFooter_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        double proprice;
        if (e.CommandName.Equals("Insert"))
        {

            TextBox p_number = gdvFooter.FooterRow.FindControl("txtpnumber") as TextBox;
            TextBox p_name = gdvFooter.FooterRow.FindControl("txtpname") as TextBox;
            TextBox p_price = gdvFooter.FooterRow.FindControl("txtpprice") as TextBox;
            if (p_price.Text == "")
            {
                proprice = 0;
            }
            else
            {
                proprice = Convert.ToDouble(p_price.Text);
            }
            SqlCommand cmd = new SqlCommand("insertproduct", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@pnumber", p_number.Text);
            cmd.Parameters.AddWithValue("@pname", p_name.Text);
            cmd.Parameters.AddWithValue("@pprice", proprice);
            cmd.Parameters.AddWithValue("@pid", SqlDbType.Int);
            cmd.Parameters["@pid"].Direction = ParameterDirection.Output;
            con.Open();
            cmd.ExecuteNonQuery();
            loadGrid();
            con.Close();
        }
    }
    protected void btnAddNewRow_Click(object sender, EventArgs e)
    {
        gdvFooter.ShowFooter = true;
        DataBind();
        loadGrid();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        gdvFooter.ShowFooter = false;
        DataBind();
        loadGrid();
    }
    protected void gdvFooter_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gdvFooter.EditIndex = e.NewEditIndex;
        loadGrid();
    }
    protected void gdvFooter_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string p_id = gdvFooter.DataKeys[e.RowIndex].Value.ToString();
        double proprice;
        //GridViewRow row = gdvFooter.Rows[e.RowIndex];
        TextBox p_numberup = gdvFooter.Rows[e.RowIndex].FindControl("txtpnumberUp") as TextBox;
        TextBox p_nameup = gdvFooter.Rows[e.RowIndex].FindControl("txtpnameUp") as TextBox;
        TextBox p_priceup = gdvFooter.Rows[e.RowIndex].FindControl("txtppriceUP") as TextBox;
        if (p_priceup.Text == "")
        {
            proprice = 0;
        }
        else
        {
            proprice = Convert.ToDouble(p_priceup.Text);
        }
        cmd = new SqlCommand("updateproduct", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@pnumber", p_numberup.Text);
        cmd.Parameters.AddWithValue("@pname", p_nameup.Text);
        cmd.Parameters.AddWithValue("@pprice", proprice);
        cmd.Parameters.AddWithValue("@pid", p_id);
        con.Open();
        cmd.ExecuteNonQuery();
        gdvFooter.EditIndex = -1;
        loadGrid();
        con.Close();
    }
    protected void gdvFooter_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string p_idD = gdvFooter.DataKeys[e.RowIndex].Value.ToString();
        string sql = "delete from product where pid=" + p_idD + "";
        cmd = new SqlCommand(sql, con);
        con.Open();
        cmd.ExecuteNonQuery();
        gdvFooter.EditIndex = -1;
        loadGrid();
        con.Close();

    }
    protected void gdvFooter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gdvFooter.EditIndex = -1;
        loadGrid();
    }
    protected void gdvFooter_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label Val1 = (Label)e.Row.Cells[1].FindControl("lbpnumber");
            Label Val2 = (Label)e.Row.Cells[3].FindControl("lbpprice");          
            Label lblSum = (Label)e.Row.Cells[4].FindControl("blbSum");

            if (Val1.Text == string.Empty || Val1.Text == "")
            {
                Val1.Text = "0";
            }
            if (Val2.Text == string.Empty || Val2.Text == "")
            {
                Val2.Text = "0";
            }
            int sum = int.Parse(Val1.Text) + int.Parse(Val2.Text);
            lblSum.Text += sum;
        }
    }
}

No comments:

Post a Comment