DataTable have a select method to select a row. This method selects a string expression by which it specifies which row you want to select. Select method spouse DataTable as like database.
First, we create a DataTable with 5 rows. Each row contains ID, NAME, and SALARY.
- DataTable table = new DataTable("Employee");
- table.Columns.Add(new DataColumn("ID", typeof(int)));
- table.Columns.Add(new DataColumn("NAME", typeof(string)));
- table.Columns.Add(new DataColumn("SALARY", typeof(int)));
-
- table.Rows.Add(1, "AMIT",8000);
- table.Rows.Add(2, "SUMIT",10000);
- table.Rows.Add(3, "RAMESH",12000);
- table.Rows.Add(4, "ROHAN",20000);
- table.Rows.Add(5, "RAHUL",5000);
-
- And we find out only those record which salary less than 10000.we use following code.
- DataRow[] result = table.Select("SALARY < 10000");
- After find out these people and update his record (salary =25000) after this we select only those record which salary >20000
- id = Convert.ToInt32(row[0]);
- string find = "ID = '" +id + "'";
-
- DataRow[] resultupdate = table.Select(find);
-
- resultupdate[0]["SALARY"] = 25000;
-
- table.AcceptChanges();
After update DataTable we call function AcceptChanges () if you want to reject last change made in DataTable you must be call RejectChanges ()