views:

24

answers:

1

Hi

I have a calendar that I generate. Currently it makes the entire month and fills each cell with a number(representing the date).

Now I want to grab values from a database and fill in the cells. How could I do this efficiently?

Like right now I can only think of grabbing the data from the database. Once that is down going through that data and essentially have like 30 if statements to determine what cell it should go into.

So that just seems like a very bad way and I am thinking of better ways. So I am wondering anyone else has any ideas.

I am using asp.net mvc I generate the body of the calendar(what is just a table) through my controller and pass it as just a string of html cells and rows.

So basically I generate in the controller all 6 rows of 7 cells(42 cells with 2 cells for previous month and remaining cells for the next month - basically looks like the windows 7 calendar) with the TagBuilder and return that as one big string.

So while building the cells that's what I would have to put the if statements to do the checking.

I am using linq to sql by the way so not sure if that will help or not.

Edit

Another way what I was thinking but not sure how to do it. Would be some how getting all the dates in range. Then take those results and do some grouping on those results. Not sure how to that kind of grouping though. It probably would not be to bad if I do the grouping on the first results and not do a request for each date and then group that. Otherwise I am looking at like 42 requests to the database to group everything.

+1  A: 

You're having to loop anyway, to build the rows and columns I assume, so why not pull the data down first, for that month, put the data into an array (old fashioned I know), and check the offset in that array as you increment through the cell rendering?

blowdart
What do you mean offset?
chobo2
Well say you have appointments[31], then you would have offset++ inside your loop, and then check appointments[offset] != null
blowdart
I still don't follow. I see that your going through the array and comparing it to null but I have compare it with like 42 different dates. Then store those dates or do something them and then eventually put them in the right cells. So I was thinking of somehow first doing all the grouping then store those groups in an array or something then finally put them in the cells.
chobo2
Yes, pull the data out limited by the month, ordered by the day. Then as you're building the cells you can check the month's data, according to the day number, and pop it in the right place.
blowdart
Ah ok I see never thought of ordering by day. I was wondering about that since they could be all over the place and you would have to like check the array everytime.
chobo2