在后面的代码中创建多个GridView [英] Create multiple gridviews in code behind
问题描述
我想在面板中使用多个GridView。并且gridviews的数量不是固定的。
所以基本上我认为在.aspx页面中应该没有代码,因为我必须在代码隐藏中创建gridview。 / p>
我在一个面板中有1个gridview的代码..其中我定义了HTML页面中的网格视图并从后面的代码中填充它。
以下是代码..可以任何1请使用多个gridviews来帮助我...
这就是。 aspx page
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =False
AllowSorting =TrueCellSpacing =2onsorting =GridView1_Sorting
Width =100%ForeColor =WhiteGridLines =None
ondatabound =GridView1_DataBound1>
<列>
< EditItemTemplate>
< asp:Label ID =Label1runat =serverText ='<%#Eval(PolicyID)%>'>< / asp:Label>
< / EditItemTemplate>
< ItemTemplate>
< asp:Label ID =Label1runat =serverText ='<%#Bind(PolicyID)%>>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>
这是背后的代码
<$ p $ ($!
if(!IsPostBack)
{
DataTable dt = new DataTable(); $ p $ {code> protected void Page_Load(object sender,EventArgs e)
SqlConnection connection = new SqlConnection(Session [ConnectionStringSQL]。ToString());
connection.Open();
SqlCommand sqlCmd = new SqlCommand(SELECT Policies.PolicyID,FROM Policies,connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
connection.Close();
if(dt.Rows.Count> 0)
{
for(int j = 0; j< dt.Rows.Count; j ++)
{
policyID.Add(dt.Rows [j] [PolicyID] .ToString());
taskTable.Columns.Add(PolicyID);
$ b $ if(policyID.Count!= 0)
{
for(int k = 0; k< policyID.Count; k ++)
{
DataRow tableRow = taskTable.NewRow();
tableRow [PolicyID] = policyID [k];
taskTable.Rows.Add(tableRow);
}
会话[TaskTable] = taskTable;
GridView1.DataSource = Session [TaskTable];
GridView1.DataBind();
}
}
}
}
没有测试过,但这种事情应该做到这一点:
for(int i = 0; i <5; i ++){
GridView gv = new GridView();
gv.DataSource = datasources [i];
Page.Controls.Add(gv);
}
Page.DataBind();
This is very interesting..
I want to have multiple gridviews in a panel. and the number of gridviews is not fixed..
So basically i think there should be no code in the .aspx page as i have to create the gridview in codebehind.
I have the code for 1 gridview in one panel.. where i define the grid view in the HTML page and populate it from the code behind.
Here is the code for that.. can any 1 please help me with the multiple gridviews...
this is on the .aspx page
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowSorting="True" CellSpacing="2" onsorting="GridView1_Sorting"
Width="100%" ForeColor="White" GridLines="None"
ondatabound="GridView1_DataBound1">
<Columns>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("PolicyID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("PolicyID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
this is the code behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(Session["ConnectionStringSQL"].ToString());
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT Policies.PolicyID, FROM Policies", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
connection.Close();
if (dt.Rows.Count > 0)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
policyID.Add(dt.Rows[j]["PolicyID"].ToString());
}
taskTable.Columns.Add("PolicyID");
if (policyID.Count != 0)
{
for (int k = 0; k < policyID.Count; k++)
{
DataRow tableRow = taskTable.NewRow();
tableRow["PolicyID"] = policyID[k];
taskTable.Rows.Add(tableRow);
}
Session["TaskTable"] = taskTable;
GridView1.DataSource = Session["TaskTable"];
GridView1.DataBind();
}
}
}
}
Not tested, but this kind of thing should do it:
for (int i=0;i<5;i++) {
GridView gv = new GridView();
gv.DataSource = datasources[i];
Page.Controls.Add(gv);
}
Page.DataBind();
这篇关于在后面的代码中创建多个GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!