[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Export DataGridView to HTML Page

Posted on 2009-04-20
3
Medium Priority
?
2,674 Views
Last Modified: 2013-12-17
Hello all. I have tried to find an answer to this question and I have not had any success. My issue is this. I have a comboBox that lists different formats to export a report to. One of those options is HTML. Basically what I want to do is take the dataGridView in a windows form and export as is to an HTML page. I would like to just export to an HTML table. I don't even know how to start on this so I don't have any sample code to provide. I am using c sharp 2008. Any advise would be greatly appreciated.
0
Comment
Question by:rsgage
3 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 24189321
I believe it's not a big trick but it depends on howyou want to display the table in html

Basicly, you just iterate through rows and columns and build html table, I guess
0
 

Author Comment

by:rsgage
ID: 24203939
That works. I am closer than I was. I can now get ti inot the HTML table but if the cell is empty it does not create the cell border. I have included the code that I used. Thanks so much for your help.
string strPageTitle = "HTML Report";
            string filler = "none";
           
            StringBuilder sb = new StringBuilder();
            StreamWriter wr = new StreamWriter(txtFile);
 
 
            sb.Append("<html>");
            sb.Append("<head>");
            sb.Append("<title>");
            sb.Append(strPageTitle);
            sb.Append("</title>");
            sb.Append("<style>");
            sb.Append(".Header {color: #FFFFFF; font-family: Verdana, Arial, Helvetica, sans-serif;font-size: x-small;font-weight: bold;}");
            sb.Append(".rows {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: x-small; }");
            sb.Append("</style>");
            sb.Append("</head>");
            sb.Append("<body>");
            sb.Append(@"<table width='90%'  border='1' align='center' cellpadding='2' cellspacing='0'>");
            
            if (dgv.Rows.Count > 0)
            {
                foreach (DataGridViewColumn col in dgv.Columns)
                {
                    if (col.Index == dgv.Columns.Count - 1)
                    {
                        sb.Append("<td>");
                        sb.Append(string.Concat(col.HeaderText));
                        sb.Append("</td>");
                        
                    }
                    else
                    {
                        sb.Append("<td>");
                        sb.Append(string.Concat(col.HeaderText));
                        sb.Append("</td>");
                    }
                }
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    //Add rows to the table for the length of the dataset
                    sb.Append("  <tr>");
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.OwningColumn.Index == dgv.Columns.Count - 1)
                        {
                            if (cell.Value != null)
                            {
                                sb.Append("<td>");
                                sb.Append(string.Concat(cell.Value.ToString(), ", "));
                                sb.Append("</td>");
                            }
                            else
                            {
                                sb.Append("<td>");
                                sb.Append(filler);
                                sb.Append("</td>");
                            }
                        }
                        else
                        {
                            if (cell.Value != null)
                            {
                                sb.Append("<td>");
                                sb.Append(string.Concat(cell.Value.ToString(), ", "));
                                sb.Append("</td>");
                            }
                            else
                            {
                                sb.Append("<td>");
                                sb.Append(string.Concat("", "none"));
                                sb.Append("</td>");
                            }
                        }
                    }
                }
                sb.Append("  </tr>");
                sb.Append("</table>");
                sb.Append("</body>");
                sb.Append("</html>");
                wr.Write(sb);
                wr.Close();

Open in new window

0
 
LVL 1

Expert Comment

by:code_me
ID: 35865395
hello
i just want to know that
what is that txtFile
in
   StreamWriter wr = new StreamWriter(txtFile);
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question