Create multiple gridviews in code behind

北慕城南 提交于 2019-12-12 05:46:06

问题


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();
                }
            }
        }
    }

回答1:


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();



回答2:


Answer 1 works but just be careful if you are nesting this inside a master page, etc.. because it seems when you just bind directly to the PAGE you cant expect it to actually be placed inside your form tag.

I have a sproc that returns unknown number of different tables that i want dumped to the page so i put a Placeholder on the aspx page

and then used the loop thru my dataset

foreach (DataTable table in ds.Tables)
{
    GridView gv = new GridView();
    gv.DataSource = table;
    gvPlaceHolder.Controls.Add(gv);
}

.....OR ....

for (int i = 0; i < ds.Tables.Count; i++)
{
           GridView gv = new GridView();
            gv.DataSource = ds.Tables[i];

            gvPlaceHolder.Controls.Add(gv);
}

.. and then bind to the placeholder so the tables end up inside my form within my child page, that is inside my master page

                gvPlaceHolder.DataBind();


来源:https://stackoverflow.com/questions/4187354/create-multiple-gridviews-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!