I have two columns in a datatable:
ID, Calls.
How do I find what the value of Calls is where ID = 5
?
5 could be anynumber, its just for example. Each row has a unique ID.
Hello just create a simple function that looks as shown below.. That returns all rows where the call parameter entered is valid or true. and simply call it as shown below; DataTable RowsFound=SearchRecords("IdColumn", OriginalTable,5);
To find a value in DataTable , use DataTable 's Select() method: DataRow[] rows = dt. Select("Column1 = 'this'"); Once you get the row(s), you can find its index using DataTable.
The row selector is a component that acts like a visual filter for datasets. It takes one dataset, chops it up into various ranges based on its configuration, and lets the user choose the splices. Then it creates a virtual dataset that only contains the rows that match the selected splices.
Make a string criteria to search for, like this:
string searchExpression = "ID = 5"
Then use the .Select()
method of the DataTable
object, like this:
DataRow[] foundRows = YourDataTable.Select(searchExpression);
Now you can loop through the results, like this:
int numberOfCalls;
bool result;
foreach(DataRow dr in foundRows)
{
// Get value of Calls here
result = Int32.TryParse(dr["Calls"], out numberOfCalls);
// Optionally, you can check the result of the attempted try parse here
// and do something if you wish
if(result)
{
// Try parse to 32-bit integer worked
}
else
{
// Try parse to 32-bit integer failed
}
}
You can use LINQ to DataSet/DataTable
var rows = dt.AsEnumerable()
.Where(r=> r.Field<int>("ID") == 5);
Since each row has a unique ID, you should use Single/SingleOrDefault
which would throw exception if you get multiple records back.
DataRow dr = dt.AsEnumerable()
.SingleOrDefault(r=> r.Field<int>("ID") == 5);
(Substitute int
for the type of your ID field)
You can try with method select
DataRow[] rows = table.Select("ID = 7");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With