Sameer Gaikwad CRUD Operation SQL stored procedure mesameergaikwad
This article was originally published on mesameergaikwad as Sameer Gaikwad net framework Gridview Edit Delete And Update In ASP.NET mesameergaikwad
A GridView is a graphical control element that presents an irregular view of data. A normal Grid View similarly supports some or all of the postdating
Clicking a column title to change the kind order of the Grid.
Dragging column heads to change their size and their order.
The GridView regulator displays the valuations of a data origin in a table. Each line represents a clearing, while each row represents a history.
Support to data source controls, similar as SqlDataSource.
Support kind capabilities.
Support update and cancel capabilities.
Support paging capabilities.
Support row selection capabilities.
Law-behind point access to the GridView object model to stoutly set parcels, handle events, and so on.
Numerous crucial fields.
Numerous data fields for the hyperlink columns.
Tailored style layout through themes and styles using CSS and javascript.
The following operations can be performed using GridView control inASP.NET usingC# law before.
Point to Bind data to GridView column
Point to Edit data in GridView
Point to Cancel rows from GridView
Point to Update row from a database
Note
The most important point that we can get by using GridView events are-RowEditing, RowUpdating, RowDeleting, RowCommand, RowDataBound, RowCancelingEdit, and Pagination.
Fields
BoundColumn To control the order and picture of columns.
HyperLinkColumn Presents the bound data in HyperLink controls
ButtonColumn Bubbles a stoner command from within a row to the grid event tutor
TemplateColumn Controls which controllers are gave up in the line
CommandField Displays Edit, Update, and revoke bangers
in take to fluxes in the
.
GridView control's EditItemIndex property.
Details Of Fields
By explicitly creating a BoundColumn in the Grid's Columns collection, the order and picture of each column can be controlled. In the BoundField parcels, when the DataField and the SortExpressions are given, sorting and rendering the data can be fluently done.
A HyperLinkColumn presents the bound data in HyperLink controllers. This is generally used to navigate from an item in the grid to a Details view on another runner by directly assigning the runner URL in NavigationUrl or by rendering it from the database.
With a TemplateColumn, the controllers which are gave up in the file and the data fields bound to the controls can be controlled. By using the TemplateColumn, any type of data control can be fitted.
The EditCommandColumn is a special column type that supports in- place editing of the data in one row in the grid. EditCommandColumn interacts with another plat of the grid EditItemIndex. By dereliction, the value of EditItemIndex is-1, meaning none of the rows ( particulars) in the grid are beingedited.However, an"edit" button is displayed in the EditCommandColumn for each of the rows in the grid, If EditItemIndex is-1.
When the"edit" selector is communed, the grid's EditCommand episode is chucked. It's over to the programmer to handle this event in the law. The typical sense sets EditItemIndex to the named row and also rebinds the data to the grid.
When EditItemIndex is set to a particular row, the EditCommandColumn shows"update"and" cancel" buttons for that row ("edit"is still displayed for the other rows). These buttons beget the UpdateCommand and CancelCommand events to be thrown, independently.
PageIndexChanging event occurs when the property of the grid AllowPaging is set to true, and in the law behind the PageIndexChanging event is fired.
Paging in GridView is allowed by sitting AllowPaging to real.
Paging in The GridView provides the means to display a group of records from the data source (for illustration, the first 20), and also navigates to the" runner" containing the coming 20 records, and so on through the data.
When enabled, the grid will display runner navigation buttons either as" coming/ former" buttons or as numeric buttons. When a runner navigation button is clicked, the PageIndexChanged event is thrown. It's over to the programmer to handle this event in the law.
The GridView fires the RowCommand episode when any push button is pressed. We can give any name as command name, and grounded on that, the check will be done and the circle will be executed.
The GridView fires the RowCreate episode when a substitute row is created.
The GridView fires the RowDeleting event when the command name is given as Cancel.
The GridView fires the RowUpdating episode when the order title is given as Update.
The GridView fires the RowEditing event when the commandment title is given away as Edit.
The GridView fires the RowDatabound episode when a data row is bound to data in a GridView control.
Data in a Grid is generally sorted by clicking the title of the column to sort. Sorting in a DataGrid can be allowed by sitting AllowSorting to real. When enabled, the grid renders LinkButton controls in the title for each column. When the button is clicked, the grid's SortCommand episode is fired. It's over to the programmer to handle this event in the law. Because DataGrid always displays the data in the same order it occurs in the data source, the typical sense sorts the data source and also rebinds the data to the grid.
CRUD operation in Gridview
CRUD is the short form of Produce, Recoup, Update, and Cancel operation. Produce, Recoup, Update, Cancel operations used in database data operation. Using these 4 operations we can perform insert, elect, modernize, and cancel SQL Garçon Database table data. We'll use these 4 operations in Gridview control in ASP.Net.
Gridview
Gridview control helps to show all SQL garçon table data in the list format. After getting the data from the SQL garçon you have to bind that list with Gridview control. For binding data with Gridview use the following 2 lines
Now use the following GridView event parcels to perform events similar to an update, cancel, edit cancel, and so on. Let us see what the parcels are
DataKeyNames This property I've used as the row indicator of GridView
OnRowEditing This property is used to handle the event when the stoner clicks on the edit button
OnRowCancelingEdit This property is used to handle the event when the stoner clicks on the Cancel button that exists after clicking on the edit button
OnRowDeleting This property is used to handle the event when the stoner clicks on the delete button that deletes the row of the GridView
OnRowUpdating This property is used to handle the event when the stoner clicks on the update button that updates the Grid Record
Websites frequently display thousands of data in a GridView in ASP.Net. Generally, the admin can view the registered druggies on the website, but when an admin wants to edit or cancel any fraud or duplicate or damaged data from the table there's a system in GridView to edit, cancel and modernize. See the note section below.
Source Code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Untitled Page</title>
- <style type="text/css">
- .Gridview {
- font-family: Verdana;
- font-size: 10pt;
- font-weight: normal;
- color: black;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" EnableModelValidation="True">
- <Columns>
- <asp:BoundField DataField="id" HeaderText="S.No." />
- <asp:BoundField DataField="name" HeaderText="Name" />
- <asp:BoundField DataField="address" HeaderText="address" />
- <asp:BoundField DataField="country" HeaderText="Country" />
- <asp:CommandField ShowEditButton="true" />
- <asp:CommandField ShowDeleteButton="true" />
- </Columns>
- <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
- <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
- <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
- <RowStyle BackColor="White" ForeColor="#003399" />
- <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
- </asp:GridView>
- </div>
- <div>
- <asp:Label ID="lblresult" runat="server"></asp:Label>
- </div>
- </form>
- </body>
- </html>
Design
The design part will look as in the following image:
Code behind
- using System;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Drawing;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- public partial class _Default: System.Web.UI.Page {
- private SqlConnection conn = new SqlConnection("Data Source=NEHASHAMA;Integrated Security=true;Initial Catalog=rp");
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- gvbind();
- }
- }
- protected void gvbind() {
- conn.Open();
- SqlCommand cmd = new SqlCommand("Select * from detail", conn);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- conn.Close();
- if (ds.Tables[0].Rows.Count > 0) {
- GridView1.DataSource = ds;
- GridView1.DataBind();
- } else {
- ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
- GridView1.DataSource = ds;
- GridView1.DataBind();
- int columncount = GridView1.Rows[0].Cells.Count;
- GridView1.Rows[0].Cells.Clear();
- GridView1.Rows[0].Cells.Add(new TableCell());
- GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
- GridView1.Rows[0].Cells[0].Text = "No Records Found";
- }
- }
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
- GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];
- Label lbldeleteid = (Label) row.FindControl("lblID");
- conn.Open();
- SqlCommand cmd = new SqlCommand("delete FROM detail where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- gvbind();
- }
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {
- GridView1.EditIndex = e.NewEditIndex;
- gvbind();
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {
- int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
- GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];
- Label lblID = (Label) row.FindControl("lblID");
- //TextBox txtname=(TextBox)gr.cell[].control[];
- TextBox textName = (TextBox) row.Cells[0].Controls[0];
- TextBox textadd = (TextBox) row.Cells[1].Controls[0];
- TextBox textc = (TextBox) row.Cells[2].Controls[0];
- //TextBox textadd = (TextBox)row.FindControl("txtadd");
- //TextBox textc = (TextBox)row.FindControl("txtc");
- GridView1.EditIndex = -1;
- conn.Open();
- //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
- SqlCommand cmd = new SqlCommand("update detail set name='" + textName.Text + "',address='" + textadd.Text + "',country='" + textc.Text + "'where id='" + userid + "'", conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- gvbind();
- //GridView1.DataBind();
- }
- protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
- GridView1.PageIndex = e.NewPageIndex;
- gvbind();
- }
- protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
- GridView1.EditIndex = -1;
- gvbind();
- }
- }
Save all or press "Ctrl+S" and hit "F5" to run the page, the page will look as in the following image:
Click on "Edit the GridView", it will display Textboxes in each cell as in the following image:
Edit the value(s) here and click on the Update link, it will update all the data, or to remove it click on the "Delete" link above the image shown.
Note
One note will be helpful for you while describing Columns in GridView if you are using a boundfield then create objects of the control using cells[index] in the rowupdateing event of GridView, but if you are using controls itself like Label or textboxes, etc then use Fincontrol("stringid").
0 Comments