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#”