hey guys and gals. I am having alot fo trouble getting my head around how to do this weird filtering thing. Ill "try" to explain it.
I have a customer table in a database with customer names. On my webpage I am displaying all customers, but I want there to be filtering in the heading of the page (just at the top).
I want the filtering to be the first letters of the existing customers in the database. But i want to group them together if there are 10 or less customers in that group.
for example: if i have 25 customers, the first 15 of them are all customers begining with "a" the rest of the customers are ranged from "b" to "z" I would want the top of the page to show:
a b-z
if i had 100 customers, the first 20 were "a"'s the next 10 were mixture of "c"'s to "m"'s the next 70 was the rest, "n" to "z" I would want the page to show
a c-m n-z
get me???
I cant do it!! I have the starting code which inistialises the SortedList (I was going to use a hashtable but didnt becuase it needed to be in order).
heres what Ive got:
SortedList sl = new SortedList();
string currentLetter = "";
foreach (DataRow row in table.Rows)
{
currentLetter = (string)row["customer_name"];
if (! sl.ContainsKey(currentLetter))
{
sl.Add(currentLetter, 0);
}
sl[currentLetter] = (int)sl[currentLetter] + 1;
}
that inisitialises the SortedList to be like:
a - 5
b - 6
d - 9
e - 14
etc...
but thats all that I can get to work successfullly.
BTW I would like the results in a new SortedList
Cheers Guys, Good Luckhere is the code I have so far, it very close but not quite:
int count = 0;
string previousLetter = "";
SortedList newList = new SortedList();
string gname = "";
int gcount = 0;
int val;
bool isGrouping = false;
foreach (string key in sl.Keys)
{
val = (int)sl[key];
count = (int)sl[key];
if (count > 10)
{
if (isGrouping)
{
if (gname == previousLetter)
newList.Add(gname, gcount);
else
newList.Add(gname + "-" + previousLetter, gcount);
}
newList.Add(key, sl[key]);
isGrouping = false;
}
else if (isGrouping)
{
if (gcount + count > 10)
{
if (gname == previousLetter)
newList.Add(gname, gcount);
else
newList.Add(gname + "-" + previousLetter, gcount);
isGrouping = false;
gcount = count;
}
else
gcount += count;
}
else // we start grouping
{
isGrouping = true;
gname = key;
gcount = count;
}
previousLetter = key;
}//end foreach
newList.Add(gname + "-" + previousLetter, gcount);
I have a customer table in a database with customer names. On my webpage I am displaying all customers, but I want there to be filtering in the heading of the page (just at the top).
I want the filtering to be the first letters of the existing customers in the database. But i want to group them together if there are 10 or less customers in that group.
for example: if i have 25 customers, the first 15 of them are all customers begining with "a" the rest of the customers are ranged from "b" to "z" I would want the top of the page to show:
a b-z
if i had 100 customers, the first 20 were "a"'s the next 10 were mixture of "c"'s to "m"'s the next 70 was the rest, "n" to "z" I would want the page to show
a c-m n-z
get me???
I cant do it!! I have the starting code which inistialises the SortedList (I was going to use a hashtable but didnt becuase it needed to be in order).
heres what Ive got:
SortedList sl = new SortedList();
string currentLetter = "";
foreach (DataRow row in table.Rows)
{
currentLetter = (string)row["customer_name"];
if (! sl.ContainsKey(currentLetter))
{
sl.Add(currentLetter, 0);
}
sl[currentLetter] = (int)sl[currentLetter] + 1;
}
that inisitialises the SortedList to be like:
a - 5
b - 6
d - 9
e - 14
etc...
but thats all that I can get to work successfullly.
BTW I would like the results in a new SortedList
Cheers Guys, Good Luckhere is the code I have so far, it very close but not quite:
int count = 0;
string previousLetter = "";
SortedList newList = new SortedList();
string gname = "";
int gcount = 0;
int val;
bool isGrouping = false;
foreach (string key in sl.Keys)
{
val = (int)sl[key];
count = (int)sl[key];
if (count > 10)
{
if (isGrouping)
{
if (gname == previousLetter)
newList.Add(gname, gcount);
else
newList.Add(gname + "-" + previousLetter, gcount);
}
newList.Add(key, sl[key]);
isGrouping = false;
}
else if (isGrouping)
{
if (gcount + count > 10)
{
if (gname == previousLetter)
newList.Add(gname, gcount);
else
newList.Add(gname + "-" + previousLetter, gcount);
isGrouping = false;
gcount = count;
}
else
gcount += count;
}
else // we start grouping
{
isGrouping = true;
gname = key;
gcount = count;
}
previousLetter = key;
}//end foreach
newList.Add(gname + "-" + previousLetter, gcount);