SIGN UP MEMBER LOGIN:    
ARTICLE

ADO.NET DataView in ASP.NET

Posted by Rohatash Kumar Articles | ADO.NET in C# February 01, 2012
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.
Reader Level:

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 like as below.

image.gif

Now insert some data into the table.

Now creating a new web application project in Visual Studio 2010. Now add the following namespace.

using System.Data.SqlClient;

using System.Data;

 

Now write the connection string to connect to the database.

 

string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";

 

Here in aspx code, I used a DataGrid.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication120.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>  

        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>   

    </div>

    </form>

</body>

</html>

 

Now we create a simple application showing the SQL Server Table Data in the GridView. The following code is the simple code without using DataView.

 

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;

 

namespace WebApplication120

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            show();

        }

        private void show()

        {

            {

                SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");

                string strSQL = "Select * from UserDetail";

                SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

                DataSet ds = new DataSet();

                dt.Fill(ds, "UserDetail");

                con.Close();

                GridView1.DataSource = ds;

                GridView1.DataBind();

            }

        }

    }

}

 

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.

 

SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");

                string strSQL = "Select * from UserDetail";

                SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

                DataSet ds = new DataSet();

                dt.Fill(ds, "UserDetail");

                con.Close();

                DataView dv = new DataView();               

                GridView1.DataSource = ds.Tables[0].DefaultView;

                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.

 

SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");

                string strSQL = "Select * from UserDetail";

                SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

                DataSet ds = new DataSet();

                dt.Fill(ds, "UserDetail");

                con.Close();

                DataView dv = new DataView(ds.Tables[0]);

                DataRowView newrow = dv.AddNew();

                newrow["UserID"] = 7;

                newrow["UserName"] = "ram";

                newrow.EndEdit();

                GridView1.DataSource = dv;

                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.

SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");

                string strSQL = "Select * from UserDetail";

                SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

                DataSet ds = new DataSet();

                dt.Fill(ds, "UserDetail");

                con.Close();

                DataView dv = new DataView(ds.Tables[0],"", "UserID", DataViewRowState .CurrentRows);

                dv.Table.Rows[4].Delete();

                GridView1.DataSource = dv;

                GridView1.DataBind();

 

Now run the application.

 

image3.gif


Resources

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor