简体   繁体   English

如何在我的代码后面的表中添加数据并以html显示?

[英]How do I add data to a table in my code behind and display in html?

I would like to pull data from my server and add to a table object. 我想从服务器中提取数据并添加到表对象中。 Loop thru the data afterwhich I would like to display the results on the aspx page. 循环遍历数据,之后我想在aspx页面上显示结果。

DataTable dTable = new DataTable();
dTable.Columns.Add("idNum", typeof(Int64));
dTable.Columns.Add("Name", typeof(String));
dTable.Columns.Add("Age", typeof(Int64));
//Connection/Command/Statement
DataReader dr = command.ExecuteReader();
while (dr.Read()) { /*Add data to rows*/ }

How do I add the data to the rows? 如何将数据添加到行? What is the best way to display on aspx? 在aspx上显示的最佳方法是什么?

write this code in the while loop 在while循环中编写此代码

DataRow drow = dTable.NewRow();

drow["id"]= dr["id"]

drow["name"]=dr["name"]

drow["age"]=dr["age"]

dTable.Rows.Add(drow);

now when your dTable source is ready , than you can bind this datasource to a DataGrid to display the data. 现在,当dTable源准备就绪时,您可以将此数据源绑定到DataGrid以显示数据。

it is not tested code. 它不是经过测试的代码。

Try this code, I tested on my local: 试试这个代码,我在本地测试过:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
        return;

    BindDG();
}

private void BindDG()
{

    SqlConnection connection = new SqlConnection("Data Source=servername;Initial Catalog=dbname;Integrated Security=True");
    using (connection)
    {
        SqlCommand command = new SqlCommand("select * From staff;",connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));

            while (reader.Read())
            {
                int id = (int)reader["id"];
                string name = reader["name"].ToString();
                int age = (int)reader["age"];

                dt.Rows.Add(id, name, age);
            }

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        else
        {
            Reponse.Write("No records found.");
        }
        reader.Close();
    }
}

您可以像这样调用Rows.Add方法

dTable.Rows.Add(id, name, age);

Use an adapter to get the data and use it to fill a DataTable then you can read the DataTable row by row and copy into an object that you can use outside of your data accessing code. 使用适配器获取数据并使用它填充数据表,然后可以逐行读取数据表并将其复制到可以在数据访问代码之外使用的对象中。

Sample to follow momentarily..... 样品可随时关注.....

EDIT: Sample added... 编辑:添加的示例...

public IList<your object> PopulateYourObject()
{
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(your connection string);
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = command;
    if (parameters != null)
    {
        // add any parameters here
    }
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adp = null;

    con.Open();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds);

    DataTable dt = ds.Tables[0]; // Now you have a DataTable

    return PopulateYourObjectListFromData(dt);
}

To Add it to the aspx I would loop throught the rows in the dt 要将其添加到aspx中,我将遍历dt中的行

    private IList<your object> PopulateYourObjectListFromData(DataTable dt)
    {
        IList<your object> newsReachArticleList = new List<your object>();
        try
        {
            foreach (DataRow row in dt.Rows)
            {
                <your object> dto = new <your object>();
                <your object>.PropertyToFill = Convert.ToString(row["column name"]);
            }
        }
    }

Then you can apply the IList to the Datasource of the DataGrid in your aspx. 然后,您可以将IList应用于aspx中的DataGrid的数据源。

public void LoadData()
{
    YourDataGrid.Datasource = PopulateYourObject();
    YourDataGrid.DataBind();
}

Check out step 2 here: http://programming.top54u.com/post/ASP-Net-Bind-GridView-to-DataTable.aspx 在此处出步骤2: http : //programming.top54u.com/post/ASP-Net-Bind-GridView-to-DataTable.aspx

Then set the datasource of your grid to your DataTable. 然后将网格的数据源设置为DataTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在HTML后面的代码中显示存储在会话变量中的值? - How can I display the value stored in my Session Variable in my code behind, in HTML? 我如何在aspx页面上显示变量值后面的代码 - How do I display the code behind variable value on my aspx page 如何在后面的代码中创建HTML文档并将其加载到WPF浏览器中 - How do I create an HTML document in my code behind and load it into a WPF browser 在后面的代码中添加控件到html表 - Add controls to a html table in code behind 如何在代码后面创建对象并在View中显示它? - How do I create objects behind code and display it in the View? 如何从后面的代码绑定并在我的数据网格中添加数据? - How to bind and add data in my datagrid from code behind? 向背后的Excel数据透视表代码添加数据 - Add data to excel pivot table code behind 如何在后面的代码中防止属性的HTML编码? - How do I prevent HTML encoding of attributes in the code behind? Silverlight-如何在后面的代码中将文本框添加到堆栈面板 - Silverlight - How do I add textboxes to the stack panel in the code behind 意外删除了Xaml文件背后的代码。 如何再次添加代码? - Accidentally deleted the code behind for an Xaml file. How do I add the code behind again?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM