Last post Jan 24, 2011 11:53 PM by MusaKoch
Jan 24, 2011 10:44 PM|MusaKoch|LINK
Before any jumps in and says, Why dont you just use a gridview, i have my reasons not to use it.
I trying to dynamically create a table and then populate the cells via a linq object.
Good news, i can dynamically create the table and the cells.
Dim numrows As Integer = MyActions.Count()
Dim numcells As Integer = 5
Dim j As Integer
For j = 0 To numrows - 1
Dim r As New TableRow()
Dim i As Integer
For i = 0 To numcells - 1
Dim c1 As New TableCell()
...? Loop through Linq Object?
However, i cant get my head around how i can assign each cell the appropriete data from the Linq object; keep in mind that it will need to loop through the linq object and populate each cell.
Below is my Linq object that i need to loop through
Dim MyActions = From Em In Linq_ConnectionDataContext_EmailNotification.ActionViews _
Where Em.OfficerID = UserID.FirstOrDefault.OfficerID _
NOTE: I have 5 cells (columns) and at this stage, this will remain as a constant. For this example, we can call the cells C1, C2, C3, C4, C5.
Jan 24, 2011 11:27 PM|shashankgwl|LINK
HtmlTable tbl = new HtmlTable();
tr = new HtmlTableRow();
tr.Cells.InnerText = "sometext";
Jan 24, 2011 11:37 PM|cyberbud|LINK
Before I jump in give you the answer for your question. Let me point out some of the benefits , why you should use gridview, as your requirement is not so complicated which compels you to create your dynamic table yourself.
1) Gridview is compatible with almost all types of datasource, even the linq datasource your are trying to use here.
2) Everything you need to write to create your dynamic table is done by gridview itself. Just give it a datasource, define columns, other extra controls such as checkbox or deletebutton. You needn't worry about that.
3) Gridview persists your data on postback, means when postback happens in your page you need not worry about maintaining the state of your table. Your dynamic table requres you to recreate it everytime you postback. Dynamic controls are not persisted on
postback as asp.net is stateless.
4) The most important thing besides persistance is sorting and paging which can be easily implemented using gridview if you use objectdatasource in your datasourceid. Just few lines of tweaking will enable you to sort your data and provide paging which may
1 day be your client's requirement. so be aware of that.
5) Good news is your gridview is rendered as Table element to your browser. If you need your data to be displayed as table and do something with it then there you have it.
5)...........more ....and more. Just go through the msdn and explore it.
Now let me answer your question.
Your linq object seems to return a list of ActionsView.
For each var actionview in MyActions
Dim r As New TableRow()
Dim c As New TableCell()
Here is what your implementation should be.
Sorry if there is anything wrong with the vbcode. I do C#.
But still I assure you and recommend you to use the gridview instead of creating table dynamically, unless your requirement cannot be met by gridview
Jan 24, 2011 11:53 PM|MusaKoch|LINK
Your example worked perfectly.
Yes, you are correct, the gridview is the obvious way to go and through out the project, i have many implentations of gridiview and i am aware of the possiblities it can achieve.
However, for my personal interest, i wanted to see if i could somehow replicate the gridview and for some coding tutorial, wanted to learn how to dynamically create a table.
The need originally originated by trying to push the table thru the HTML reader method which i had issues with when it came to formatting and how the final email (pushed the results to the body of an email) looked. I was hoping that if i constructed it by
a plain table, it may keep its formatting.
Having said this, it may be the HTML reader or even the email body causing me the inconsisted formatting ..
Thanks again for your help.