Link to home
Start Free TrialLog in
Avatar of fm250
fm250Flag for United States of America

asked on

C# dynamic table cell/rows

what is wron with my table rows/cell. it only display the last var assigined which "summary" from the xml. note that looping through xml is correct.

I know I am not looping correctly. thanks!


Table tbl = new Table();
        tbl.ID = "table1";
        tbl.BorderWidth = 1;
        this.Controls.Add(tbl);


        var xmlDoc = XDocument.Load(new XmlTextReader(Server.MapPath("NewsSrc.xml")));
        foreach (var descendant in xmlDoc.Descendants("NewsItem"))
        {
                      
            TableRow rw = new TableRow();
            TableCell cell = new TableCell();
            var title = descendant.Element("Title").Value;                          
            var summary = descendant.Element("Summary").Value;
            var details = descendant.Element("Details").Value;
           
            cell.Text = title; 
            rw.Cells.Add(cell);
            tbl.Controls.Add(rw);

            cell.Text = summary;
            rw.Cells.Add(cell);
            tbl.Controls.Add(rw); 
        }


--------------xml -----
    <NewsItem id="1">
        <Title> news tile one </Title>
        <Summary> this is summary to show ....</Summary>
        <Details>details for this news</Details>   
</NewsItem>

    <NewsItem id="2">
        <Title>test title </Title>
        <Summary>sm line</Summary>
        <Details> no details</Details>    
    </NewsItem>

Open in new window

Avatar of Bill Nolan
Bill Nolan
Flag of United States of America image

You are overwriting the same cell, and adding the rows incorrectly.

Create a new cell for 'title' and 'summary' text.
Add the rows to Table.Rows, e.g.: tbl.Rows.Add(rw);
change the for loop code like below

foreach (var descendant in xmlDoc.Descendants("NewsItem"))
        {
                     
            TableRow rw = new TableRow();
            TableCell cell = new TableCell();
            var title = descendant.Element("Title").Value;                          
            var summary = descendant.Element("Summary").Value;
            var details = descendant.Element("Details").Value;
           
            cell.Text = title;
            rw.Cells.Add(cell);


            cell.Text = summary;
            rw.Cells.Add(cell);

            tbl.Controls.Add(rw);

        }

you only need to add the row variable rw to table tbl variable once.
no need to add it more times
there is another mistake
you declaring the cell only once

change the for loop code like below

foreach (var descendant in xmlDoc.Descendants("NewsItem"))
        {
                     
            TableRow rw = new TableRow();
            TableCell cell = new TableCell();
            var title = descendant.Element("Title").Value;                          
            var summary = descendant.Element("Summary").Value;
            var details = descendant.Element("Details").Value;
           
            cell.Text = title;
            rw.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = summary;
            rw.Cells.Add(cell);

            tbl.Controls.Add(rw);

        }

you need to reinitialize the cell variable for each field
Avatar of fm250

ASKER

gmailjini,
the last one works except that I need it to be under the title in another row, not in  cell/column. thanks!
Avatar of nishant joshi
create a dataset and add one datatable to it..


named as "NewsItem"
and add colums to it. 1)"Title"                          
                                2)"Summary"
                                3)"Details"


then you have to called just a one method of NewsItemDataTabel.ReadXML("xmlfilepath");
so you need to add a column captions for the table ?
Avatar of fm250

ASKER

Now it is like this: see (1)
-----------------------------------
| title    |   summary            |
-----------------------------------
| title    |  summary             |
-----------------------------------

I need it to be like this

-----------------------------------
|  title                                  |
-----------------------------------
|  summary                         |
-----------------------------------
-----------------------------------
| title                                  |
-----------------------------------
|  summary                        |
-----------------------------------

thanks!
ASKER CERTIFIED SOLUTION
Avatar of Jini Jose
Jini Jose
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fm250

ASKER

that works. Just one quick other thing before I close and accept the answer.

when it parse a new paragraph from the xml, it doesn't leave the new line.
is there a way to leave the new line (parse it to the html).

thanks!
Here is the program for you...check it.


Regards,
nishant
DSNewsItems.NewsItemDataTable dtnewsitem = new DSNewsItems.NewsItemDataTable();
            dtnewsitem.ReadXml("NewsSrc.xml");

Open in new window

yes you can add a blank row to the table just like below

rw = new TableRow();
cell = new TableCell();
rw.Cells.Add(cell);
cell = new TableCell();
rw.Cells.Add(cell);
tbl.Controls.Add(rw);