asp.net实例代码之添加DataColumn到DataTable控件中
本文导语: asp.net添加DataColumn数据列到DataTable控件的例子。 代码: 代码示例: void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) BindData(); } IEnumerable ItemsInCart() { ...
asp.net添加DataColumn数据列到DataTable控件的例子。
代码:
void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindData();
}
IEnumerable ItemsInCart()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ProductID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Price", typeof(float)));
for (int i = 0; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i+1;
dr[1] = "Item " + i.ToString();
dr[2] = (float) 1.2 * i;
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
string GetProductDescription(int ID)
{
return "This is the description for product " + ID.ToString() + "...";
}
void BindData()
{
dgCart.DataSource = ItemsInCart();
dgCart.DataBind();
}
void dgCart_ItemCommand(object sender, DataGridCommandEventArgs e)
{
Response.Write("A button was clicked...");
}