.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

CRUD operation in GridView

Posted By:Ashutosh Jha       Posted Date: June 09, 2014    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

Here i am going to explain how to load gridview from tables, how to edit a row and update it.
 

Please create a page like gridoperation.aspx and take a grid view from data in a tool bar. This page will look something like the below-

  <asp:GridView ID="gv" runat="server" BackColor="White"  DataKeyNames="Id" CellPadding="1" CellSpacing="1" CssClass="DHTR_Grid_Row"
          BorderColor="Aqua" Font-Size="Medium" Width="950px" GridLines="Both"  
          RowStyle-BorderColor="Aquamarine" RowStyle-BorderWidth="2px"
                  HeaderStyle-BackColor="Aqua" AutoGenerateColumns="False" 
          BorderWidth="2px" onrowediting="gv_RowEditing" 
          onrowupdating="gv_RowUpdating" onrowcancelingedit="gv_RowCancelingEdit">
                      <Columns>


                          <asp:BoundField DataField="Id" HeaderText="ID" />
                          <asp:BoundField DataField="Heading" HeaderText="Promo Name" />
                          <asp:BoundField DataField="Details" HeaderText="Details" />
                          <asp:BoundField DataField="Expires" HeaderText="Expiry Date" />
                          <asp:CommandField HeaderText="Update" ShowEditButton="True" />

                      </Columns>
                  
                  </asp:GridView>       
     
<asp:Label ID="lblresult" runat="server" ></asp:Label>

And then write the code behind the page as below-
 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn2"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
         {
       gvbind();
             //BindEmployeeDetails();
         }
    }
    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gv.EditIndex = e.NewEditIndex;
        gvbind();
    }
    protected void gvbind()
    {

        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from repdetails", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gv.DataSource = ds;
            gv.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gv.DataSource = ds;
            gv.DataBind();
            int columncount = gv.Rows[0].Cells.Count;
            gv.Rows[0].Cells.Clear();
            gv.Rows[0].Cells.Add(new TableCell());
            gv.Rows[0].Cells[0].ColumnSpan = columncount;
            gv.Rows[0].Cells[0].Text = "No Records Found";
        }
    }
    protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(gv.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)gv.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");

        TextBox textName = (TextBox)row.Cells[1].Controls[0];
        TextBox textadd = (TextBox)row.Cells[2].Controls[0];
        TextBox textc = (TextBox)row.Cells[3].Controls[0];
        DateTime d = Convert.ToDateTime(textc.Text);

        gv.EditIndex = -1;
        con.Open();

        SqlCommand cmd = new SqlCommand("update repdetails set Heading='" + textName.Text + "',Details='" + textadd.Text + "',Expires='" + d + "'where id='" + userid + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        gvbind();
        lblresult.Text = "Record updated successfully !!";
    }
    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gv.EditIndex = -1;
        gvbind();
    }

    

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend