locked
Dynamically creating a table and then populating the cell RRS feed

  • Question

  • User-1576352007 posted

    Hi all,

    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?
          
                    r.Cells.Add(c1)
    
                Next i
                Table2.Rows.Add(r)
            Next j


    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 _
            Select Em


    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.

    Thanks

     

    Monday, January 24, 2011 10:44 PM

Answers

  • User1415119982 posted

    Dear MusaKoch,

    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()
                   
                   c.Text=actionview.column1
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   Table2.Rows.Add(r)
    
                next


    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


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 24, 2011 11:37 PM

All replies

  • User377791177 posted

    HtmlTable tbl = new HtmlTable();

    tr = new HtmlTableRow();

    tr.Cells.Add(new HtmlTableCell("colA"));

    tr.Cells.Add(new HtmlTableCell("colB"));

    tr.Cells[0].InnerText = "sometext";

    Monday, January 24, 2011 11:27 PM
  • User1415119982 posted

    Dear MusaKoch,

    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()
                   
                   c.Text=actionview.column1
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   c=new TableCell()
                   c.Text=actionview.column2
                   r.Cells.Add(c)
                   Table2.Rows.Add(r)
    
                next


    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


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 24, 2011 11:37 PM
  • User-1576352007 posted

    CyberBud,

    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.

    Monday, January 24, 2011 11:53 PM