0

私はこのASPXコードを持っています:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;


public partial class Default4 : System.Web.UI.Page
{int i;
    public string strAdmin;
    public string strPass;
    public string strLname;
    public string strEmail;
    public string strFname;
    public string str;

    protected void Page_Load(object sender, EventArgs e)
    {
        string dbPath = Server.MapPath(@"App_Data") + "/my_site.mdb";
        string connectionString = @"Data Source='" + dbPath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
        OleDbConnection con = new OleDbConnection(connectionString);
        con.Open();
        string QueryString = "SELECT * FROM tbl_users";
        OleDbCommand cmd = new OleDbCommand(QueryString, con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "tbl");
        con.Close();
        strt = 
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            strFname = ds.Tables[0].Rows[i]["first_name"].ToString();
            strLname = ds.Tables[0].Rows[i]["last_name"].ToString();
            strEmail = ds.Tables[0].Rows[i]["user_email"].ToString();
            strPass = ds.Tables[0].Rows[i]["user_password"].ToString();
            strAdmin = ds.Tables[0].Rows[i]["is_admin"].ToString();
            str += String.Format("{0}&nbsp{1}&nbsp{2}&nbsp{3}&nbsp{4}", strFname, strLname, strEmail, strPass, strAdmin);
        }
    }
}

この情報をテーブルに表示して、より役立つようにするにはどうすればよいですか?
(どの html タグをここに追加する必要がありますか? どこに追加しますか?) 助けてください。

4

1 に答える 1

1

@Davidが述べたように、このタイプの機能のためにDataGridにデータバインドする必要があります。ただし、本当にそのデータセットをhtmlテーブルにレンダリングしたい場合(スタイリング、強烈なJavaScriptなどのさまざまな理由で)、この関数を使用してデータセットのhtml表現を返し、divに戻りhtmlを入力します。

    public static string BuildHTMLTable(DataSet dataSet)
    {
        var table = dataSet.Tables[0];
        var tableString = "<table>";

        tableString += "<thead>";

        for (var i = 0; i < table.Columns.Count; i++)
        {
            tableString += "<th>" + table.Columns[i].ColumnName + "</th>";
        }
        tableString += "</thead>";
        tableString += "<tbody>";

        for(var x = 0; x < table.Rows.Count; x++)
        {
            tableString += "<tr>";

            for (var y = 0; y < table.Columns.Count; y++)
            {
                tableString += "<td>";
                tableString += table.Rows[x][y];
                tableString += "</td>";
            }


            tableString += "</tr>";
        }

        tableString += "</tbody>";
        tableString += "</table>";

        return tableString;
    }
于 2012-11-19T19:55:07.597 に答える