如何从后面的代码创建gridview的gridview和boundfields [英] How to create gridview and boundfields of gridview from code behind
本文介绍了如何从后面的代码创建gridview的gridview和boundfields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在asp.net
How to create gridview and boundfields of gridview from code behind in asp.net
推荐答案
后面的代码中创建gridview和gridview的boundfields一旦完成此代码
once go through this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
GridView GrdDynamic=new GridView ();
protected void Page_Load(object sender, EventArgs e)
{
loadDynamicGrid();
panelname.Controls.Add(GrdDynamic);
}
private void loadDynamicGrid()
{
#region Code for preparing the DataTable
//Create an instance of DataTable
DataTable dt = new DataTable();
//Create an ID column for adding to the Datatable
DataColumn dcol = new DataColumn(ID ,typeof(System.Int32));
dcol.AutoIncrement = true;
dt.Columns.Add(dcol);
//Create an ID column for adding to the Datatable
dcol = new DataColumn("NAME", typeof(System.String));
dt.Columns.Add(dcol);
//Now add data for dynamic columns
//As the first column is auto-increment, we do not have to add any thing.
//Let's add some data to the second column.
for (int nIndex = 0; nIndex < 10; nIndex++)
{
//Create a new row
DataRow drow = dt.NewRow();
//Initialize the row data.
drow["NAME"] = "Row-" + Convert.ToString((nIndex + 1));
//Add the row to the datatable.
dt.Rows.Add(drow);
}
#endregion
//Iterate through the columns of the datatable to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
//Add the newly created bound field to the GridView.
GrdDynamic.Columns.Add(bfield);
}
//Initialize the DataSource
GrdDynamic.DataSource = dt;
//Bind the datatable with the GridView.
GrdDynamic.DataBind();
}
}
}
在aspx页面写这个
write this in aspx page
<body>
<form id="form1" runat="server">
<div id="testgrid" runat="server">
<asp:panel runat="server" id="pnael34" xmlns:asp="#unknown">
</asp:panel>
</div>
</form>
</body>
</body>
这篇关于如何从后面的代码创建gridview的gridview和boundfields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文