Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one dataset in c#. in which i have some rows with type a and type b. now i want to count the number of rows per type in c# in ado.net.
Posted
Comments
Karthik_Mahalingam 23-Jan-14 23:51pm    
what to you mean by Type ? , datatable column has a datatype which is common to all rows ...
can u pls add more info...
ravikhoda 24-Jan-14 0:19am    
something like this
column1 column2 column3
ABC TypeA 10
XYZ TypeB 10
ABC1 TYPEA 20
XYZ1 TYPEA 15
CCC TYPEB 10

this should return type a count = 3 and type b count = 2. want to do with table.select method or something.
Karthik_Mahalingam 24-Jan-14 1:02am    
try my solution.

If your question is this , then will work for you..


C#
DataTable dt = new DataTable();
       dt.Columns.Add("Types", typeof(string));
       dt.Rows.Add("TypeA");
       dt.Rows.Add("TypeB");
       dt.Rows.Add("TypeA");

       int TypeACount = dt.Rows.OfType<DataRow>().Count(k => k["Types"].ToString() == "TypeA");
       int TypeBCount = dt.Rows.OfType<DataRow>().Count(k => k["Types"].ToString() == "TypeB");
 
Share this answer
 
Comments
Karthik_Mahalingam 24-Jan-14 1:03am    
int TypeACount = dt.Rows.OfType<datarow>().Count(k => k["column2 "].ToString() == "TypeA");
int TypeBCount = dt.Rows.OfType<datarow>().Count(k => k["column2 "].ToString() == "TypeB");
C#
int rowCounter = 0;
foreach(DataRow dr in dts.Tables[0].Rows)
{
    rowCounter++;
}

Response.Write("" + rowCounter);

Hope you get the basic idea from here, this will iterate all the rows of your table and increase the count.
I have simply printed the count, but you can use that wherever you want!!

Good Luck !
 
Share this answer
 
private DataSet SplitRows(DataTable source)
{
 
  DataSet ds = new DataSet();
 
  foreach(DataRow r in source.Rows) 
  {
    DataTable copy = source.Clone();
    copy.LoadDataRow(r.ItemArray, true);
    ds.Tables.Add(copy);
  }
 
  return ds;
 
}
try this
 
Share this answer
 
VB
Dim ds As New System.Data.DataSet()
Dim table As New System.Data.DataTable
table.Columns.Add("sno", System.Type.GetType("System.Int32"))
table.Columns.Add("sname", System.Type.GetType("System.String"))
table.Columns.Add("fee", System.Type.GetType("System.Int32"))
table.Rows.Add(1, "", 1)
table.Rows.Add(2, "ad", 9)
table.Rows.Add(3, "wre", 90)
ds.Tables.Add(table)


For i As Int16 = 0 To ds.Tables(0).Columns.Count - 1
    Response.Write(table.Columns(i).DataType())

Next
 
Share this answer
 
Hi,


Dataset ds=new Dataset();

Bind data to Dataset..

int a=0;
int b=0;

for(int i=0;i<ds.tables[0].rows.count;>{
if(ds.Tables[0].Rows[0].ItemArray[1].ToString() =="TypeA")
{
a = a + 1;
}
else if(ds.Tables[0].Rows[0].ItemArray[1].ToString() =="TypeB")
{
b = b + 1;
}
}

return

string TypeA =a;
string TypeB =b;
 
Share this answer
 
i manange to solve this myself using below code.

C#
string expression = "type='TypeA'";
DataRow[] result = DT.Select(expression);
int Count = result.Lenght; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900