How to process DataTable using DataView in C#
Process DataTable using DataView in C#
In this article we will see how to use
DataView for process data off line. In case of disconnected architecture we pull data in memory and then process it for further operation. To do
same we will use DataView. By using DataView we can make operation on
DataTable.
Sort DataTable using Sort property of DataView
Data fetched from database or any
other data source may not in sorted order. Any way after storing in DataTable
we can arrange it according to column value using sort property.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("roll", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(4, "sourav");
dt.Rows.Add(3, "ram");
dt.Rows.Add(1, "Shyam");
dt.Rows.Add(2, "Hari");
dt.DefaultView.Sort = "roll";
this.dataGridView1.DataSource = dt;
}
}
}

AND and OR Condition
If we want
to fetch particular row depending on search criteria then we can use searching
mechanism on DataTable.
private void button1_Click_1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("roll", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(4, "sourav");
dt.Rows.Add(3, "ram");
dt.Rows.Add(1, "Shyam");
dt.Rows.Add(2, "Hari");
DataView dv = new DataView(dt, "name = 'sourav' OR roll='3' " , "name", DataViewRowState.CurrentRows);
this.dataGridView1.DataSource
= dv;
}

Here we
have used name=’sourav’ OR roll=3 in search condition.
Count Property
If we want to know how many rows are
there in DataTable then we have to use count property of DefaultView.
private void button1_Click_1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("roll", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(4, "sourav");
dt.Rows.Add(3, "ram");
dt.Rows.Add(1, "Shyam");
dt.Rows.Add(2, "Hari");
int Rows = dt.DefaultView.Count;
this.dataGridView1.DataSource = dt;
this.label1.Text = "Number of Student:- " + Rows;
}

Find row by primary key
If DataTable has any primary key then
using Find() method we can pick any row from DataTable like below.
private void button1_Click_1(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("roll", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.PrimaryKey = new DataColumn[] { dt.Columns["roll"] };
dt.Rows.Add(4, "sourav");
dt.Rows.Add(3, "ram");
dt.Rows.Add(1, "Shyam");
dt.Rows.Add(2, "Hari");
DataRow r = dt.Rows.Find("1");
this.label1.Text ="Roll :- " + r["roll"].ToString()
+ "Name:- " + r["name"].ToString();
}
We have to pass value of primary key
column of DataTable as a argument of Find() function.
