views:

48

answers:

3

In my code behind I have put an query result in dataset which returns a table like as bellow:

Sl_NO    COMPLEATED_ON      SCORE      IS_PRESENT      MAX_VALUE
1         29/07/2010          4            0              12
2         29/07/2010          5            0              13
3         29/07/2010          6            1              23
4         29/07/2010          7            1              44
5                             6            1
6                             5            0
7                             4            1

My reqirement is that I need to count total number non empty rows which column name is COMPLEATED_ON.I mean from above example it should return 4 since rest three rows of the column COMPLEATED_ON is empty.Can any one tell me how to do this in C#?

+1  A: 

You can use the Select method of the DataTable:

DataTable table = DataSet.Tables[tableName];

string expression = "COMPLEATED_ON IS NOT NULL AND COMPLEATED_ON <> ''";
DataRow[] foundRows = table.Select(expression);

int rowCount = foundRows.Length;

Or here's a brute force way:

int count = 0;

DataTable table = DataSet.Tables[tableName];
foreach (DataRow row in table.Rows)
{
   string completed = (string)row["COMPLEATED_ON"];
   if (!string.IsNullOrEmpty(completed))
   {
      count++;
   }
}
GenericTypeTea
+1  A: 

Try this:

dsDataSet.Tables[0].Select("COMPLEATED_ON is not null").Length;
rosscj2533
A: 

You could use LINQ to determine this:

var nonEmptyRows = (from DataRow record in myDataSet.Tables[0].AsEnumerable()
                   where !string.IsNullOrEmpty(record.Field<string>("COMPLEATED_ON"))
                   select record).Count();

nonEmptyRows would then be an int giving you the count of non-empty rows.

Rob