vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Sunday, December 4, 2011

Update,edit and delete in grid view using C# code Or how to edit itemtemplate in gridview using c# code


Introduction:
Here I have write an  article about update,delete and edit in grdview using C# coding….Already I have written in vb code …For your reference I mention the link below…….

Description:
Below its fully an client side code…

   <div>
        <asp:GridView ID="GridView1" runat="server" Width="500px"
            AutoGenerateColumns ="false" AllowPaging ="true" GridLines ="Both"
            AlternatingRowStyle-BackColor ="AliceBlue"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
            DataKeyNames ="id" onrowdeleting="GridView1_RowDeleting">
        <Columns>
        <asp:TemplateField HeaderText ="student Name">
        <ItemTemplate >
        <%#Eval("Name") %>
              
        </ItemTemplate>
        <EditItemTemplate >
            <asp:TextBox ID="TextBox1" runat="server" Text=' <%#Eval("Name") %>' ></asp:TextBox>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Address">
        <ItemTemplate >
        <%#Eval("address") %>
       
       
        </ItemTemplate>
        <EditItemTemplate >
            <asp:TextBox ID="TextBox2" runat="server" Text=' <%#Eval("address") %>' ></asp:TextBox>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Gender">
        <ItemTemplate >
        <%#Eval("sex") %>
       
       
        </ItemTemplate>
        <EditItemTemplate >
            <asp:TextBox ID="TextBox3" runat="server" Text=' <%#Eval("sex") %>' ></asp:TextBox>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Remarks">
        <ItemTemplate >
        <%#Eval("remarks") %>
       
       
        </ItemTemplate>
        <EditItemTemplate >
            <asp:TextBox ID="TextBox4" runat="server" Text=' <%#Eval("remarks") %>' ></asp:TextBox>
        </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton ="true" />
        <asp:TemplateField HeaderText ="Delete" >
        <ItemTemplate >
                    <asp:Button ID="Button1" runat="server" Text="Delete" CommandName ="delete"/>

        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
               
        </asp:GridView>
    </div>
Below is server side code…
protected void Page_Load(object sender, EventArgs e)
    {
       
        con.Open();

        Loadgr();
      
        con.Close();
   }

    void  Loadgr()
   {
       strold = "select * from stud";
       OleDbCommand oldcmd = new OleDbCommand(strold,con);
       OleDbDataAdapter oldda = new OleDbDataAdapter(oldcmd);
        DataSet ds=new DataSet();
       oldda.Fill(ds);
       GridView1.DataSource = ds;
       GridView1.DataBind();


}

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txt1;
        TextBox txt2;
        TextBox txt3;

        TextBox txt4;
        int id;
        id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        txt1 = GridView1.Rows[GridView1.EditIndex].FindControl("textbox1") as TextBox;
        txt2 = GridView1.Rows[GridView1.EditIndex].FindControl("textbox2") as TextBox;
        txt3 = GridView1.Rows[GridView1.EditIndex].FindControl("textbox3") as TextBox;
        txt4 = GridView1.Rows[GridView1.EditIndex].FindControl("textbox4") as TextBox;

        //txt4=((TextBox )GridView1.FindControl("textbox1"));
        strold = "update stud set Name='" + txt1.Text + "',Address='" + txt2.Text + "',Sex='" + txt3.Text + "',Remarks='" + txt4.Text + "' where ID='" + id + "'";
        OleDbCommand oldcmd = new OleDbCommand(strold,con);
        con.Open();
        oldcmd .ExecuteNonQuery ();
        con.Close();
            Loadgr ();



    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Loadgr();

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Loadgr();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id;
        id = Convert.ToInt32(GridView1 .DataKeys [e.RowIndex ].Value );
        strold ="delete from stud where ID'" + id +"'";
        OleDbCommand oldcmd=new OleDbCommand (strold ,con);
        con.Close ();
        oldcmd.ExecuteNonQuery();

        con.Close ();
        Loadgr ();



    }
Post Your comments here………



0 comments:

Post a Comment