ADO.NET DataView in ASP.NET

A DataView provides various views of the data stored in a DataTable. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression. That is we can customize the views of data from a DataTable.
 

Creating Table in SQL Server Database

 
Now create a table named UserDetail with the columns UserID and UserName. The table looks as below.
 
image.gif
 
Now insert some data into the table.
 
Now create a new web application project in Visual Studio 2010. Now add the following namespace.
  1. using System.Data.SqlClient;  
  2. using System.Data; 
Now write the connection string to connect to the database.
  1. string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";  
Here in aspx code, I used a DataGrid.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication120.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.      <head runat="server">  
  6.           <title></title>  
  7.      </head>  
  8.   
  9.      <body>  
  10.           <form id="form1" runat="server">  
  11.                <div>  
  12.     <asp:GridView ID="GridView1" runat="server">  
  13.     </asp:GridView>  
  14.                </div>  
  15.           </form>  
  16.      </body>  
  17.   
  18. </html> 
Now we create a simple application showing the SQL Server Table Data in the GridView. The following code is a simple code without using DataView.
  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.   
  10. namespace WebApplication120 {  
  11.     public partial class WebForm1: System.Web.UI.Page {  
  12.         protected void Page_Load(object sender, EventArgs e) {  
  13.             show();  
  14.         }  
  15.         private void show() {  
  16.             {  
  17. SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");  
  18. string strSQL = "Select * from UserDetail";  
  19. SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);  
  20. DataSet ds = new DataSet();  
  21. dt.Fill(ds, "UserDetail");  
  22. con.Close();  
  23. GridView1.DataSource = ds;  
  24. GridView1.DataBind();  
  25.             }  
  26.         }  
  27.     }  

Creating a DataView

 
To convert a DataSet to a DataView in ASP.Net using C# code, you can initialize the DataView class object by accessing the DefaultView property via DataTable collection of DataSet. DefaultView property enables you to convert the DataSet to DataView.
  1. SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");  
  2. string strSQL = "Select * from UserDetail";  
  3. SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);  
  4. DataSet ds = new DataSet();  
  5. dt.Fill(ds, "UserDetail");  
  6. con.Close();  
  7. DataView dv = new DataView();  
  8. GridView1.DataSource = ds.Tables[0].DefaultView;  
  9. GridView1.DataBind(); 
Now run the application.
 
image1.gif
 

Adding new row in the DataView

 
We can add new rows in the DataView using AddNew() method in the DataView. The following C# source code shows how to add a new row in a DataView.
  1. SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");  
  2. string strSQL = "Select * from UserDetail";  
  3. SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);  
  4. DataSet ds = new DataSet();  
  5. dt.Fill(ds, "UserDetail");  
  6. con.Close();  
  7. DataView dv = new DataView(ds.Tables[0]);  
  8. DataRowView newrow = dv.AddNew();  
  9. newrow["UserID"] = 7;  
  10. newrow["UserName"] = "ram";  
  11. newrow.EndEdit();  
  12. GridView1.DataSource = dv;  
  13. GridView1.DataBind(); 
Now run the application. we will see row has been added in the DataView.
 
image2.gif
 

Delete rows in a DataView

 
We can add new rows in the DataView using Delete() method in the DataView. The following C# source code shows how to delete rows in a DataView.
  1. SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");  
  2.  string strSQL = "Select * from UserDetail";  
  3. SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);  
  4. DataSet ds = new DataSet();  
  5. dt.Fill(ds, "UserDetail");  
  6. con.Close();  
  7. DataView dv = new DataView(ds.Tables[0],"""UserID", DataViewRowState .CurrentRows);  
  8. dv.Table.Rows[4].Delete();  
  9. GridView1.DataSource = dv;  
  10. GridView1.DataBind(); 
Now run the application.
 
image3.gif
 
Resources


Similar Articles