In this tutorial, I’ll show you how to make an editable GridView in ASP.NET.
Here, I have taken three Views. On the first View, I am inserting Student Name and City to my database. On the second View, I am inserting Student Course and Student semester to my same database, and on the last View, I have an editable GridView to show the details of every student.
INITIAL CHAMBER
Step 1
Open your Visual Studio 2010 and create an Empty Website. Give it a suitable name [data_demo].
Step 2
In Solution Explorer, you get your empty website. Add a web form and SQL Database by going like this –
For Web Form
data_demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as data_demo.aspx.
For SQL Server Database
data_demo (Your Empty Website) -> Right Click -> Add New Item -> SQL Server Database. [Add Database inside the App_Data_folder].
DATABASE CHAMBER
Step 3
Go to your Database [Database.mdf], and create a table tbl_data1. Go to the database.mdf - -> Table - -> Add New table, and design your table like this.
Stored Procedures
Sp_Insert()
- CREATE PROCEDURE[dbo].[sp_insert1]
-
- for the stored procedure here(@id int output, @name varchar(50) = 'Nilesh', @city varchar(50) = 'Rajkot')
- AS
- insert into tbl_data1(name, city) values(@name, @city)
- select @id = id from tbl_data1 where name = @name and city = @city
- RETURN
Sp_update()
- CREATE PROCEDURE[dbo].[sp_update]
-
- for the stored procedure here(@id int, @name varchar(50), @city varchar(50))
- AS
- update tbl_data1
- set
- name = @name,
- city = @city
- where id = @id
- RETURN
DESIGN CHAMBER
Step 4
Open your data_demo.aspx file to create the design. We will drag a Mutiview Control and inside that, take three Views as Student Details, Student Course, and Student Summary respectively.
Here, inside the third View, we have taken GridView control, so that if any edition is needed for the student data, they can do that. Your design will look like the below image. If you find any difficulty in making this design, I have also shared my code too. You can have a look at that.
CODE CHAMBER
Step 5
Open data_demo.aspx.cs file to write code for saving the partial data in your database table and also the editable GridView.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- using System.Data.Sql;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
-
- public partial class Multiview : System.Web.UI.Page
- {
- static int id1 = 0;
- SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- MultiView1.ActiveViewIndex = 0;
- }
-
- }
-
- protected void Button2_Click(object sender, EventArgs e)
- {
- MultiView1.ActiveViewIndex = 1;
-
-
- SqlCommand cmd = new SqlCommand("sp_insert1", con);
-
- cmd.CommandType = CommandType.StoredProcedure;
- SqlParameter outpara = new SqlParameter("@id", SqlDbType.Int);
- outpara.ParameterName = "@id";
- outpara.SqlDbType = System.Data.SqlDbType.Int;
- outpara.Direction = System.Data.ParameterDirection.Output;
- cmd.Parameters.Add(outpara);
- cmd.Parameters.AddWithValue("@name", textname.Text);
- cmd.Parameters.AddWithValue("@city", textcity.Text);
- con.Open();
- cmd.ExecuteNonQuery();
- id1 = Convert.ToInt32(outpara.Value is DBNull ? default(int) : outpara.Value);
- con.Close();
- refreshdata();
-
- }
-
- public void refreshdata()
- {
- SqlCommand cmd = new SqlCommand("select * from tbl_data1", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- GridView1.DataSource = dt;
- GridView1.DataBind();
-
- }
-
- protected void Button4_Click(object sender, EventArgs e)
- {
- MultiView1.ActiveViewIndex = 2;
-
- }
-
- protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- GridView1.EditIndex = -1;
- refreshdata();
-
- }
-
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
- con.Open();
- SqlCommand cmd = new SqlCommand("delete from tbl_data1 where id =@id", con);
- cmd.Parameters.AddWithValue("id", id);
- int i = cmd.ExecuteNonQuery();
- con.Close();
- refreshdata();
-
- }
-
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
- {
- GridView1.EditIndex = e.NewEditIndex;
- refreshdata();
-
- }
-
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox;
- TextBox txtcity = GridView1.Rows[e.RowIndex].FindControl("TextBox5") as TextBox;
- TextBox txtcourse = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
- TextBox txtsem = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
- int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
- con.Open();
- SqlCommand cmd = new SqlCommand("sp_update", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("name", txtname.Text);
- cmd.Parameters.AddWithValue("city", txtcity.Text);
- cmd.Parameters.AddWithValue("course", txtcourse.Text);
- cmd.Parameters.AddWithValue("sem", txtsem.Text);
- cmd.Parameters.AddWithValue("id", id);
-
- int i = cmd.ExecuteNonQuery();
- con.Close();
- GridView1.EditIndex = -1;
- refreshdata();
-
- }
-
- protected void Button3_Click(object sender, EventArgs e)
- {
- MultiView1.ActiveViewIndex = 2;
- SqlCommand cmd = new SqlCommand("update tbl_data1 set course= @course,sem= @sem where id =" + id1.ToString(), con);
- cmd.Parameters.AddWithValue("@course", TextBox6.Text);
- cmd.Parameters.AddWithValue("@sem", TextBox7.Text);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- refreshdata();
-
- }
-
- protected void Button4_Click1(object sender, EventArgs e)
- {
- MultiView1.ActiveViewIndex = 0;
- }
- }
The code is relatively very easy to understand. To make the GridView, I have taken its updating, editing, deleting, and canceling event and inside the event, I have put my CRUD operation code. For storing the data from two Views in one table, you can refer to my storing some data in table tutorial to get a better View on the topic.
OUTPUT CHAMBER
Okay! Here, we will insert name and city from the first View and see if it is saving in the database or not.
First View
Tbl_data - Name and City
Second View
Check this on the database to see if it is stored properly or not.
Okay! Let’s proceed to next summarized View.
It's perfect. I am sorry for this dummy data in the GridView. I have tested it so many times; that’s why it is showing like that. Moreover, you can edit and delete it respectively.
Hope you like it. Thank you for reading. Have a good day.