473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

loop through dataset

How can i loop through my dataset if if colum 1 is there more then once
group them together

example:
dataset looks like this
BMW
BMW
BMW
LEXUS
LEXUS

I want to show BMW once, and Lexus 1 but count the number of time they are
there
so

like this as my output
Cars Sales
BMW 3
LEXUS 2


Dec 6 '06 #1
4 4671
Hi..I hav one approach which somehow finds the result..dont know
whether it is the right way or not..i am writing down the code for it..
Hashtable hs = new Hashtable();
ArrayList arr = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
try
{
hs.Add(dr["Brand"],string.Empty); //only one BMW
will be added here
}
catch
{
arr.Add(dr["Brand"]); //duplicate values will be
added here
}
}
Response.Write(hs.Count); //will show the no of duplication
items for each product
Response.WriteLine(arr.Count);

If u will find any other solution just post it n group..
Thanks..

igotyourdotnet wrote:
How can i loop through my dataset if if colum 1 is there more then once
group them together

example:
dataset looks like this
BMW
BMW
BMW
LEXUS
LEXUS

I want to show BMW once, and Lexus 1 but count the number of time they are
there
so

like this as my output
Cars Sales
BMW 3
LEXUS 2
Dec 7 '06 #2
Hello Mike,

If you're forced to do this in .net code layer with a given Dataset in the
below data format:

=========
BMW
BMW
BMW
LEXUS
LEXUS
==========

I think you may need to manually loop each DataRow in the DataTable and
store the calculated(grouped result set) into a new DataTable. e.g

=====test page code=====
protected void btnClick_Click(object sender, EventArgs e)
{
DataTable tb = GetTargetTable(GetSourceTable());

GridView1.DataSource = tb;

GridView1.DataBind();
}

//calculate the group result
private DataTable GetTargetTable(DataTable dt)
{
DataTable newtb = new DataTable("target");
newtb.Columns.Add("Cars");
newtb.Columns.Add("Sales", typeof(Int32));

string currentCar = null;

foreach (DataRow row in dt.Rows)
{
string car = row["Cars"] as string;

DataRow newrow = null;

if (currentCar == null || currentCar != car)
{

currentCar = car;
newrow = newtb.NewRow();
newrow["Cars"] = car;
newrow["Sales"] = 1;
newtb.Rows.Add(newrow);
}
else
{
DataRow currentrow = newtb.Rows[newtb.Rows.Count - 1];
currentrow["Sales"] = (int)currentrow["Sales"] + 1;
}
}

return newtb;

}
//simulate the source table
private DataTable GetSourceTable()
{
DataTable dt = new DataTable("source");
dt.Columns.Add("Cars");

dt.Rows.Add("BMW");
dt.Rows.Add("BMW");
dt.Rows.Add("BMW");
dt.Rows.Add("LEXUS");
dt.Rows.Add("LEXUS");
dt.Rows.Add("PASSAT");
dt.Rows.Add("PASSAT");
dt.Rows.Add("LEXUS");
dt.Rows.Add("TOYOTA");
dt.Rows.Add("TOYOTA");

return dt;

}
==================================

BTW, if this is possible to do at database layer, it will be much easier to
use SQL to group the original data.

Hope this helps some.
Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Dec 7 '06 #3
HOW TO: Implement a DataSet GROUP BY Helper Class
http://support.microsoft.com/kb/325685

I think there is a C# counterpart there also.
"igotyourdotnet" <ig************@nospam.nospamwrote in message
news:ed**************@TK2MSFTNGP02.phx.gbl...
How can i loop through my dataset if if colum 1 is there more then once
group them together

example:
dataset looks like this
BMW
BMW
BMW
LEXUS
LEXUS

I want to show BMW once, and Lexus 1 but count the number of time they are
there
so

like this as my output
Cars Sales
BMW 3
LEXUS 2


Dec 7 '06 #4
Hello sloan!

Have you tried dataview for that purpose?
Develope an expression using DataColumn.Expression property.

Hope that would solve your problem & do let me know in any case.

Regards,
Mohsin Rizvi

sloan wrote:
HOW TO: Implement a DataSet GROUP BY Helper Class
http://support.microsoft.com/kb/325685

I think there is a C# counterpart there also.
"igotyourdotnet" <ig************@nospam.nospamwrote in message
news:ed**************@TK2MSFTNGP02.phx.gbl...
How can i loop through my dataset if if colum 1 is there more then once
group them together

example:
dataset looks like this
BMW
BMW
BMW
LEXUS
LEXUS

I want to show BMW once, and Lexus 1 but count the number of time they are
there
so

like this as my output
Cars Sales
BMW 3
LEXUS 2


Dec 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MrMike | last post by:
I have a Public Function which requires 2 parameters. Public Function X (ByVal FirstParam as String, ByVal SecondParam as String) This function displays various options for the 2 parameters(delete,...
4
by: edpdc | last post by:
VB .NET I would like to know how to loop through each row in a data set and get a value from a colum in the data set. My data set has multiple columns. I know I have to use the datarow class but...
2
by: dSchwartz | last post by:
I need help adding a column to a dataset, but its a little bit more complicated then just that. Here's the situation: I have many xml files in one directory, each which represent a newsletter. I...
3
by: Mark | last post by:
Hi - when working with datasets, is it quicker to loop through the dataset, comparing some column values with predetermined values, or should I apply a filter on the dataset to retrieve the values...
1
by: Shapper | last post by:
Hello, I have this loop: For Each row As DataRow In dsContent("pagename").Tables(0).Rows Dim div As HtmlGenericControl = CType(Page.FindControl(row("tag_name").ToString), HtmlGenericControl)...
4
by: CMW | last post by:
In VB.NET I'm retrieving a single table from a SQL Server (tblformdata). I need to search each record in the dataset to see if any data is missing from the records (i.e., missing first name,...
3
by: Rudy | last post by:
I am writing a program in VB.NET and as I was debugging a problem I noticed my For loop doesn't want to loop! I originally had a upper bound which was an expression and it wasn't working. WHen I...
7
by: Nitromuse | last post by:
What is the proper way to refer to a dataset as the collection in a For Each, Next Statement? I want to loop through a particular column in the dataset, I've tried the following with no sucess. ...
2
by: Philip Wagenaar | last post by:
I have a dataset that I queried from an excel sheet. I want to loop through the rows, then loop through the columns and write them to a file. But I got stuck pretty quickly: So far: For Each...
16
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataAdapter. For every record in tblA where colB = 'abc', I want to update the value in colA. In VB6, using ADO I can loop thru the recordset,set the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.