Sunday, 22 April 2012

Edit ,Update, Delete record in Gridview and develop manually gridview.

Default.aspx----
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="grid1" DataKeyNames="student_id,college_name" runat="server"
AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowcancelingedit="grid1_RowCancelingEdit"
onrowdeleting="grid1_RowDeleting" onrowediting="grid1_RowEditing"
onrowupdating="grid1_RowUpdating"
onrowcommand="grid1_RowCommand" Height="15px" Width="861px"
onrowdatabound="grid1_RowDataBound" CellPadding="5"
       
> 
    
<AlternatingRowStyle Wrap="False" />
    
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="update1" CommandName="Update" runat="server" ImageUrl="~/images1/Edit.gif"  Height="15px" Width="15px" />
<asp:ImageButton ID="cancel1" runat="server" CommandName="Cancel" ImageUrl="~/images1/close.png"  Height="15px" Width="15px" />

</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="edit1" CommandName="Edit" runat="server" ImageUrl="~/images1/Edit.gif"  Height="15px" Width="15px" />
<asp:ImageButton ID="delete1" CommandName="Delete" Text="Edit" runat="server" ImageUrl="~/images1/delete_icon.gif"  Height="15px" Width="15px" />
</ItemTemplate>
<FooterTemplate>


</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="College Name">
<EditItemTemplate>
<asp:Label ID="colbl" runat="server" Text='<%#Eval("collage_name") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="colbl" runat="server" Text='<%#Eval("collage_name") %>'/>
</ItemTemplate>
<FooterTemplate>

</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:TextBox ID="st1" runat="server" Text='<%#Eval("student_name") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="st1" runat="server" Text='<%#Eval("student_name") %>'/>
</ItemTemplate>
<FooterTemplate>

</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<EditItemTemplate>
<asp:TextBox ID="add" runat="server" Text='<%#Eval("address") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="add" runat="server" Text='<%#Eval("address") %>'/>
</ItemTemplate>
<FooterTemplate>

</FooterTemplate>
</asp:TemplateField>


</Columns>
<EditRowStyle Wrap="False" />
<FooterStyle Wrap="False" />

<HeaderStyle BackColor="Olive" Font-Bold="False" ForeColor="White" Wrap="False"></HeaderStyle>
<PagerStyle Wrap="False" />
<RowStyle Wrap="False" />
<SelectedRowStyle Wrap="False" />
</asp:GridView>

</div>

<div align="center">
<asp:Label ID="lblresult" runat="server"></asp:Label>
</div>
</div>  </ContentTemplate></asp:UpdatePanel>


Javascript coding-------
<script type="text/javascript">
function popup(student_name) {

    var result = confirm(' you sure you want to delete ' + student_name + ' panku?');
if (result) {

return true;
}
else {
return false;
}
}
</script>

Default.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 System.Configuration;
using System.Drawing;
using System.Text.RegularExpressions;
public partial class Default : System.Web.UI.Page
{
   
    private SqlConnection con = new SqlConnection("give connectionstring");
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
            Bindpanku();
        }
    }




    protected void Bindpanku()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from panku", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            grid1.DataSource = ds;
            grid1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            grid1.DataSource = ds;
            grid1.DataBind();
            int columncount = grid1.Rows[0].Cells.Count;
            grid1.Rows[0].Cells.Clear();
            grid1.Rows[0].Cells.Add(new TableCell());
            grid1.Rows[0].Cells[0].ColumnSpan = columncount;
            grid1.Rows[0].Cells[0].Text = "No Records Found";
        }

    }
    protected void grid1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid1.EditIndex = e.NewEditIndex;
        Bindpanku();
    }


    protected void grid1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int Project_codestudent_id = Convert.ToInt32(grid1.DataKeys[e.RowIndex].Value.ToString());
        string college_name = grid1.DataKeys[e.RowIndex].Values["college_name"].ToString();
        TextBox st11 = (TextBox)grid1.Rows[e.RowIndex].FindControl("st1");
        TextBox add1 = (TextBox)grid1.Rows[e.RowIndex].FindControl("add");
       



        con.Open();
        SqlCommand cmd = new SqlCommand("update panku set student_name='" + st11.Text + "',address='" + add1.Text + "',   where student_id=" + student_id, con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = college_name + " panku Updated successfully";
        grid1.EditIndex = -1;
        Bindpanku();
    }



    protected void grid1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grid1.EditIndex = -1;
        Bindpanku();
    }

    protected void grid1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int student_id = Convert.ToInt32(grid1.DataKeys[e.RowIndex].Values["student_id"].ToString());
        string college_name = grid1.DataKeys[e.RowIndex].Values["college_name"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from panku where student_id=" + student_id, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            Bindpanku();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = college_name + " details deleted successfully";
        }
    }



    protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //getting username from particular row
            string college_name = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "college_name"));
            //identifying the control in gridview
            ImageButton result = (ImageButton)e.Row.FindControl("delete1");
            //raising javascript confirmationbox whenver user clicks on link button
            if (result != null)
            {
                result.Attributes.Add("onclick", "javascript:return popup('" + college_name + "')");
            }

        }
    }
}

No comments:

Post a Comment

What is ASP.NET? Components of ASP.NET

ASP.Net Definition -It is used for creating web-based applications.ASP.Net is a web development platform provided by Microsoft. ASP.NET i...