Dataview in C#

Working with dataview concepts:

 

1)dataview  is a predefined class, which is the part of system.data namespace

 

2)dataview is used to retrieve the desired data from a dataset based on a condition

 

3)dataview filter the data with the help of rowfilter property

 

4)syntax to create dataview object

 

   Dataview dv=ds.Tables[“d”].defaultview;

 

5)dataview is used only for filtering the data but not for storing the data

 

6)dataview filters the data with help of rowfilter property

 

 Syntax of rowfilter:

 

    dv.rowfilter=”column name=value”

 

 

 

example on dataview

 

 

 

Open windows forms app project

 

Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->

 

project->select visual c# from installed templates->select windows forms application project 

 

 

 

place textbox

 

place datagrid view control

 

place 4 linklabel buttons with text A,B,>7300, All

 

using system.Data.OleDb;

 

 

 

using System;

 

using System.Collections.Generic;

 

using System.ComponentModel;

 

using System.Data;

 

using System.Drawing;

 

using System.Linq;

 

using System.Text;

 

using System.Windows.Forms;

 

using System.Data.OleDb;

 

 

 

namespace WindowsFormsApplication24

 

{

 

    public partial class Form1 : Form

 

    {

 

        public Form1()

 

        {

 

            InitializeComponent();

 

        }

 

        DataSet ds = new DataSet();

 

        DataView dv;

 

 

 

        private void Form1_Load(object sender, EventArgs e)

 

        {

 

            OleDbConnection cn = new OleDbConnection("provide=msdaora.1;user id=scott;password=tiger");

 

            OleDbDataAdapter da = new OleDbDataAdapter("select * from emp", cn);

 

            da.Fill(ds, "d");

 

            dv = ds.Tables["d"].DefaultView;

 

            dataGridView1.DataSource = dv;

 

 

 

        }

 

 

 

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs       e)

 

          {

 

            dv.RowFilter = "ename like 'a%'";

 

        }

 

 

 

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

 

        {

 

            dv.RowFilter = "ename like 'b%'";

 

        }

 

 

 

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

 

        {

 

            dv.RowFilter = "emno >7300";

 

        }

 

 

 

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

 

        {

 

            dv.RowFilter = "";

 

        }

 

 

 

        private void textBox1_TextChanged(object sender, EventArgs e)

 

        {

 

            dv.RowFilter = "ename like" + textBox1.Text + "%'";

 

        }

 

    }

 

}

 

 

tags:

Dataview C#