-2

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 ?

KamalF
  • 105
  • 1
  • 3
  • 19
  • I don't understand your question. `myColumn` is a single `DataColumn`. A columns belongs to a table. A table can have multiple `DataRows` and every row has values for every column. But in total the number of rows for one column is the same as the number of rows in the table. – Tim Schmelter Sep 29 '15 at 09:38
  • yes my question was i want to know the number of rows in this column to be able to check this in a comparison function – KamalF Sep 29 '15 at 11:27
  • Every column has the same number of rows as the table itself, so `myColumn.Table.Rows.Count`. Your question is still pointless, isn't it? – Tim Schmelter Sep 29 '15 at 11:28
  • Thanks, i will check it. – KamalF Sep 29 '15 at 11:29
  • Why do i keep getting negative reviews !! and somebody added an answer and was it then he edited my question and removed the answer !! – KamalF Sep 29 '15 at 11:31
  • You got downvotes because you haven't clarified the question. _"get the datarow count in the dataColumn"_ seems pointless since a columns cannot have rows but the table. So people were confused what you actually want. – Tim Schmelter Sep 29 '15 at 11:34

1 Answers1

0

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
    // ....
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thanks please check my question again, hopefully it is clearer – KamalF Sep 29 '15 at 11:50
  • @KamalF: now your requirement seems to be different. You want to find a value in a string column. What value? What do you want to do with matching rows? Just check if the value exists or return matching rows? – Tim Schmelter Sep 29 '15 at 11:55
  • just check if the value exists and if so, it returns the value, if not it returns empty string variable – KamalF Sep 29 '15 at 11:57
  • @KamalF: i would return `null` if the value doesn't exist. Otherwise you can't differentiate between a found empty string or a not found empty string(if you search that value). However, why do you want to return a value that you are searching instead of just a `bool` that indicates whether the table contained it or not? What is the value/variable that you're searching? – Tim Schmelter Sep 29 '15 at 11:59
  • Or do you want to return the row-count of the matching rows? Then i understand your original question. – Tim Schmelter Sep 29 '15 at 12:00
  • Actually i wanted to return both a bool and the value at the beginning but it seemed to be difficult and i though about only a value and if it is not then it would be considered as false bool. The idea is that i am searching for a type of data in that table for presence. If it is present it should be represented in a label in a view of the MVC application. So after this comparison the return will be passed to the View by Json and presented for the user. Is it clear now ? – KamalF Sep 29 '15 at 12:03