Editable MultiView In ASP.NET Using C#

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.

ASP.NET

Stored Procedures

Sp_Insert() 

  1. CREATE PROCEDURE[dbo].[sp_insert1]  
  2.     --Add the parameters  
  3. for the stored procedure here(@id int output, @name varchar(50) = 'Nilesh', @city varchar(50) = 'Rajkot')  
  4. AS  
  5. insert into tbl_data1(name, city) values(@name, @city)  
  6. select @id = id from tbl_data1 where name = @name and city = @city  
  7. RETURN  

Sp_update()

  1. CREATE PROCEDURE[dbo].[sp_update]  
  2.     --Add the parameters  
  3. for the stored procedure here(@id int, @name varchar(50), @city varchar(50))  
  4. AS  
  5. update tbl_data1  
  6. set  
  7. name = @name,  
  8.     city = @city  
  9. where id = @id  
  10. 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.

ASP.NET

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. using System.Data.Sql;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Web.UI.HtmlControls;  
  12.   
  13. public partial class Multiview : System.Web.UI.Page  
  14. {  
  15.     static int id1 = 0;  
  16.     SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");  
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.     {  
  19.         if (!IsPostBack)  
  20.         {  
  21.             MultiView1.ActiveViewIndex = 0;  
  22.         }  
  23.   
  24.     }  
  25.   
  26.     protected void Button2_Click(object sender, EventArgs e)  
  27.     {  
  28.         MultiView1.ActiveViewIndex = 1;  
  29.   
  30.           
  31.         SqlCommand cmd = new SqlCommand("sp_insert1", con);  
  32.          
  33.         cmd.CommandType = CommandType.StoredProcedure;  
  34.         SqlParameter outpara = new SqlParameter("@id", SqlDbType.Int);  
  35.         outpara.ParameterName = "@id";  
  36.         outpara.SqlDbType = System.Data.SqlDbType.Int;  
  37.         outpara.Direction = System.Data.ParameterDirection.Output;  
  38.         cmd.Parameters.Add(outpara);        
  39.         cmd.Parameters.AddWithValue("@name", textname.Text);  
  40.         cmd.Parameters.AddWithValue("@city", textcity.Text);  
  41.         con.Open();  
  42.         cmd.ExecuteNonQuery();  
  43.         id1 = Convert.ToInt32(outpara.Value is DBNull ? default(int) : outpara.Value);  
  44.         con.Close();  
  45.         refreshdata();  
  46.   
  47.     }  
  48.   
  49.     public void refreshdata()  
  50.     {    
  51.         SqlCommand cmd = new SqlCommand("select * from tbl_data1", con);  
  52.         SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  53.         DataTable dt = new DataTable();  
  54.         sda.Fill(dt);  
  55.         GridView1.DataSource = dt;  
  56.         GridView1.DataBind();  
  57.   
  58.     }  
  59.   
  60.     protected void Button4_Click(object sender, EventArgs e)  
  61.     {  
  62.         MultiView1.ActiveViewIndex = 2;        
  63.          
  64.     }  
  65.   
  66.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  67.     {  
  68.         GridView1.EditIndex = -1;  
  69.         refreshdata();  
  70.   
  71.     }  
  72.   
  73.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  74.     {  
  75.        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());  
  76.         con.Open();  
  77.         SqlCommand cmd = new SqlCommand("delete from tbl_data1 where id =@id", con);  
  78.         cmd.Parameters.AddWithValue("id", id);  
  79.         int i = cmd.ExecuteNonQuery();  
  80.         con.Close();  
  81.         refreshdata();  
  82.   
  83.     }  
  84.   
  85.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)  
  86.     {  
  87.         GridView1.EditIndex = e.NewEditIndex;  
  88.         refreshdata();  
  89.   
  90.     }  
  91.   
  92.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  93.     {  
  94.         TextBox txtname = GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox;  
  95.         TextBox txtcity = GridView1.Rows[e.RowIndex].FindControl("TextBox5") as TextBox;  
  96.         TextBox txtcourse = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;  
  97.         TextBox txtsem = GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;  
  98.         int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());  
  99.         con.Open();  
  100.         SqlCommand cmd = new SqlCommand("sp_update", con);  
  101.         cmd.CommandType = CommandType.StoredProcedure;  
  102.         cmd.Parameters.AddWithValue("name", txtname.Text);  
  103.         cmd.Parameters.AddWithValue("city", txtcity.Text);  
  104.         cmd.Parameters.AddWithValue("course", txtcourse.Text);  
  105.         cmd.Parameters.AddWithValue("sem", txtsem.Text);  
  106.         cmd.Parameters.AddWithValue("id", id);  
  107.   
  108.         int i = cmd.ExecuteNonQuery();  
  109.         con.Close();  
  110.         GridView1.EditIndex = -1;  
  111.         refreshdata();  
  112.   
  113.     }  
  114.   
  115.     protected void Button3_Click(object sender, EventArgs e)  
  116.     {  
  117.         MultiView1.ActiveViewIndex = 2;  
  118.         SqlCommand cmd = new SqlCommand("update tbl_data1 set course= @course,sem= @sem where id =" + id1.ToString(), con);   
  119.         cmd.Parameters.AddWithValue("@course", TextBox6.Text);  
  120.         cmd.Parameters.AddWithValue("@sem", TextBox7.Text);  
  121.         con.Open();  
  122.         cmd.ExecuteNonQuery();  
  123.         con.Close();  
  124.         refreshdata();  
  125.   
  126.     }  
  127.   
  128.     protected void Button4_Click1(object sender, EventArgs e)  
  129.     {  
  130.         MultiView1.ActiveViewIndex = 0;  
  131.     }  
  132. }  

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

ASP.NET

Tbl_data - Name and City

ASP.NET

Second View

ASP.NET

Check this on the database to see if it is stored properly or not.

ASP.NET

Okay! Let’s proceed to next summarized View.

ASP.NET
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.

Up Next
    Ebook Download
    View all
    Learn
    View all