How to fetch data rows from a data view in C#


Data View is a representation of data table which can be used to access rows in a table. In Database programming sometimes we need to search and extract/copy rows which meet some criteria/condition. C# and .Net framework allows you to do this using rich set of objects available in Visual Studio.

In our example we have a table Groups which is used to store various accounting groups. I want to extract those groups with pgid[Primary Group ID] as 0, this is made possible with Data View object’s FindRows method.

Common.GroupTableView.Sort = "pgid";
 dv.Sort = "pgid";
 DataRowView[] nodes = Common.GroupTableView.FindRows(0);

FindRows () return a collection of data rows, so that we used array of dataRowView to store the objects.

You can use Foreach loop to get access values as follows [ add a tree view control to your C# Project]

foreach (DataRowView node in nodes)
 {
 treeView1.Nodes[0].Nodes.Add(node["name"].ToString()); 
 }
 treeView1.ExpandAll();

3 thoughts on “How to fetch data rows from a data view in C#

  1. Pingback: Set IList as ComboBox Datasource in C# | DeveloperM – A developer's Manual

  2. Pingback: How to use dataView rowfilter for searching rows in C#.Net | DeveloperM – A developer's Manual

  3. Pingback: How to create a DataView in C# | DeveloperM – A developer's Manual

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.