 |
|
 |
Does this tutorial have any C# code? I am just starting become a web programmer, and not sure how to convert it to C#?
|
|
|
|
 |
|
 |
Do you want to try it out yourself? Change VB to C# is like change code format. If you still want a c# version, I will try to do it for you after these couple of days.
|
|
|
|
 |
|
 |
Thanks for your answer and reply, i'll will try to do it. But, it you have free time to do it please post the C# version. Thanks you very much.
|
|
|
|
 |
|
 |
OK.
I have a 7-month-old daughter. If she is not so cute and seductive and time-consuming, I would do this earlier.
|
|
|
|
 |
|
 |
Maybe you already figured out? Anyway, the following is the code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<html>
<Script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
int i,j;
//Create instance of DataSet object
DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath("Employees.xml"));
int rowCount = DS.Tables[0].Rows.Count;
int colCount = DS.Tables[0].Columns.Count;
//create a two-dimensional array
String[][] dataArray = new String[rowCount][];
//save data in a two dimensional array
for (i = 0; i < rowCount;i++)
{
dataArray[i] = new String[colCount];
for (j = 0; j < colCount; j++)
{
dataArray[i][j] =DS.Tables[0].Rows[i][j].ToString();
}
}
//Switch columns and rows, dynamically filling in the table
TableRow r;
TableCell c;
for (j=0; j < colCount; j++)
{
r= new TableRow();
c = new TableCell();
c.Controls.Add(new LiteralControl(DS.Tables[0].Columns[j].ColumnName));
c.VerticalAlign = VerticalAlign.Top;
c.Style["background-color"] = "lightblue";
r.Cells.Add(c);
for (i = 0; i
the rest is the same
|
|
|
|
 |
|
 |
Thank you very much for your help. I have some problem in the multi dimension array (from dataArray(i,j) to dataArray[i][j])). Finally, wish you have a happiness family.
The code missing some word on line 47, it should be like this
for (i = 0; i < rowCount; i++) {
|
|
|
|
 |
|
 |
Thanks a lot . This code is really helpful. I am using WinForms instead of WebForms since I am developing a stand alone app. I get an error:
The type or namespace name 'TableRow' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'TableCell' could not be found (are you missing a using directive or an assembly reference?)
Any suggestions
Navvy
|
|
|
|
 |
|
 |
Hi, I think tables are not for winforms at all. Right now, only one thing comes to my mind, that is you can dynamically add labels. But it is pretty cumbersome. Or you may do some sort of crystal report, but I am not sure either.
Sorry, if i thought of something else, I will let you know
|
|
|
|
 |
|
 |
public static Table GetVerticalTable(DataTable data, Table table) { int rowCount = data.Rows.Count; int colCount = data.Columns.Count; string[,] dataArray = new string[rowCount, colCount]; for (int i = 0; i <= rowCount - 1; i++) { for (int j = 0; j <= colCount - 1; j++) { if (data.Rows[i].ItemArray[j] == DBNull.Value) { dataArray[i, j] = ""; } else { dataArray[i, j] = data.Rows[i].ItemArray[j].ToString(); } } } TableRow r; TableCell c; for (int j = 0; j <= colCount - 1; j++) { r = new TableRow(); c = new TableCell(); c.Controls.Add(new LiteralControl(data.Columns[j].ColumnName)); c.VerticalAlign = VerticalAlign.Top; c.Style["background-color"] = "lightblue"; r.Cells.Add(c); for (int i = 0; i <= rowCount - 1; i++) { c = new TableCell(); c.Controls.Add(new LiteralControl(dataArray[i, j])); c.VerticalAlign = VerticalAlign.Top; r.Cells.Add(c); } table.Rows.Add(r); } return table; <div class="ForumMod">modified on Thursday, July 31, 2008 6:47 PM</div>
|
|
|
|
 |
|
 |
Thank you so much for posting this. Your code is exactly what I needed and has saved me at least 3 hours of dev time!!
Bonnie
|
|
|
|
 |
|
 |
Why not simply export data as xml (using scheduled batch job or asp code in the page) and then use XSL to show them the way you want ? This way, you put the load on the client, greatly simplify so called layout code and centralize it in one place.
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
 |
|
 |
Thanks.
You mean the transformed data or original data? The original data source is a xml file. It was written from a database table using xmlwriter. Isn't that true we still need to traverse the xml data node by node? I cannot see how .xsl can solve the problem. Or am i missing something? Please advise
|
|
|
|
 |
|
 |
Once you have original data in xml file, you can use a XSL stylesheet to transform it. In XSL file, depending on how you use for-each and value-of statements to traverse the XML file, you can go row by row or column by column.
You can state the XSL file to be used directly into the xml file using <?xml-stylesheet type="text/xsl" href="toto.xsl"?> and the browser will output a temp html file in the cache on the client.
ASP/PHP/CGI/etc. are good when you need to add/modify data, but for displaying it, I usually pick a solution involving XSL. The only drawback is that not all browser support it (eg. Opera support is lacking).
Tell me if you need more details ...
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
 |
|
 |
Thanks. It indeed is another short cut to accomplish the same thing. I wonder if it is possible to write the xsl dynamically? I added
a second solution accordingly. Also,is there another way to render the xsl, more efficient and straightforward?
Thanks a lot, again
|
|
|
|
 |
|
 |
My pleasure really When I first discovered XSL, I was amazed what you could accomplish with it.
At its base, XSL is just XML so all you can do with XML, you can do with XSL. It follows that you can use System.Xml to generate it dynamically.
That said, this would be in contradiction with the reason why you would use it in the first place : using DOM engine is expensive and most often than not, overkill. System.Xml.XmlTextWriter would be the most efficient way to generate the XSL since it is non-cached and forward-only.
Take a look at this excellent tutorial site about XSL/XSLT ... There are many built-in functions supported by XSL ! And then, you can judiciously use XPath to locate and traverse data.
You ask for a way to render more efficiently ... Since it's the client's browser engine that do all the work (internally, IE use exactly the same library you would use in code), I don't see how you could do better except by optimizing the XSL itself.
If you don't want to link the XML file directly to the XSL, which is not really a good way to do things I agree, check here to learn how to use Javascript instead. This would even let you specify different XSL stylesheets depending on browsers or other variables.
Also, an alternative would be to use System.Xml.Xsl.XslTransform class to do the transformation, but this way, it is done on server side.
Also, note that you can reference CSS files in XSL, further separating presentation layers and allowing reuse.
Wooo ... ! As you can see, one can easily get lost with all those technologies And this is only the tip of the iceberg
Sébastien
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
 |
|
 |
this code should be re-usable?
so why you use fix columnames? this is not necessary,
you can read them from the dataset.
regards
SeeSharp
|
|
|
|
 |
|
|
 |
|
 |
Thank you very much. I have changed the code accordingly.
|
|
|
|
 |
|
 |
This is something I've always thought would be useful in a windows forms based app.
-k
|
|
|
|
 |
|
 |
Hi curry,
your code works well.
thank you!
Mary
|
|
|
|
 |