[英]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.