This
article will show you how you can apply where clause or filter in a datatable
using c#.net and vb.net.
So for this article we will create a new console application to perform an example. So first we will have a datatable.
So for this article we will create a new console application to perform an example. So first we will have a datatable.
private static DataTable FillStudentData()
{
DataTable dtstudent = new DataTable();
//Adding columns to table Emp
DataColumn
colEmpid = new DataColumn("RollNo", typeof(System.Int32));
DataColumn
colName = new DataColumn("Name", typeof(System.String));
DataColumn
colDept = new DataColumn("Address", typeof(System.String));
//Adding columns to datatable
dtstudent.Columns.AddRange(new DataColumn[] { colEmpid, colName, colDept });
//Adding data
dtstudent.Rows.Add(1, "John Smith
Brown", "Address 1");
dtstudent.Rows.Add(2, "Carry
Brown", "Address 2");
dtstudent.Rows.Add(3, "Candle
Pencil", "Address 3");
dtstudent.Rows.Add(4, "Graham
Bell", "Address 4");
dtstudent.Rows.Add(5, "Peter
Kevin", "Address 5");
return dtstudent;
}
|
Now add the
below code.
C#.NET:
DataTable dtstudent = new DataTable();
dtstudent = FillStudentData();
DataRow[] filterData1 =
dtstudent.Select("RollNo=3");
foreach (DataRow item in filterData1)
{
Console.WriteLine("Roll No : "
+ item.Field<int>("RollNo") + ", Name :
" + item.Field<string>("Name") + ", Address : "
+ item.Field<string>("Address"));
}
|
VB.NET:
Dim dtstudent As New DataTable()
dtstudent = FillStudentData()
Dim filterData1 As DataRow() =
dtstudent.[Select]("RollNo=3")
For Each item As DataRow In filterData1
Console.WriteLine("Roll
No : " + item.Field(Of Integer)("RollNo") + ", Name : " +
item.Field(Of String)("Name") + ", Address : " +
item.Field(Of String)("Address"))
Next
|
In above
code check the highlighted part of the code. In this I have used select and
passed the value to check the record for roll no 3. So after execution we will
get the below output.
0 comments:
Please let me know your view