I have a datatable which has 3 columns "Name","Type","ID" i want to use only one column in it for comparison. Which is "Type". I wanted to take that column put it in a DataColumn separated from the datatable, compare the values of the cells in it with a string and then pass a string if it matches. I am not sure if this method is right or wrong, i even dont know how to get the string value of the cells (rows) in this Datacolumn to be compared
Here is my code for it :
DataTable Griddetails;
Griddetails = RxMUaClient.BrowseGrid("2", "127.0.0.1:48030", nodepassed);
var myColumn = Griddetails.Columns.Cast<DataColumn>()
.SingleOrDefault(col => col.ColumnName == "Type");
return Json(myColumn);
if (myColumn != null)
{
for (int i = 0; i <= myColumn.; i++ )
{
}
}
I am trying to get the datarow count in the dataColumn either by a for loop or foreach, the foreach is not working for some error and the i don't know how to get the value of the datacolumn for the for loop.
Any help ?
Even if the question still doesn't make much sense to me, if you just want to know how many rows a table has that belongs to a given DataColumn
:
int rowCount = myColumn.Table.Rows.Count;
Note that you don't need the LINQ query to find the column, you can use the DataColumnCollection
indexer that takes a string:
DataColumn myColumn = Griddetails.Columns["Type"];
It works as desired if there is no column with that name and returns null
.
After your edited question it seems that you actually want to find row(s) which type-column contain a given value. You can use LINQ:
var matchingRows = from row in Griddetails.AsEnumerable()
where row.Field<string>("Type") == valueYouSearch
select row;
DataRow firstRow = matchingRows.FirstOrDefaul();
if(firstRow != null)
{
// here you know that there is at least one row with that value in the type-column
string firstType = firstRow.Field<string>("Type"); // pointless, maybe you want to return different column's value
// ....
}