本文介绍了如何动态添加gridviews使用asp.net c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个页面,显示几个GridView中的一些细节。我在说几个,因为gridviews的数量不是。我在aspx页面上有一个面板,并动态地向它添加gridviews。

aspx code:

 < asp:Panel ID = pnlResultrunat =server>< / asp:Panel> 

aspx.cs code

  int numOfGroups =这里有一些数字; 
for(int i = 1; i< numOfGroups + 1; i ++)
{
GridView grd = new GridView();
grd.ID =GridView+ i.ToString();
grd.BackColor = getColor(i);
grd.DataSource = dt; //一些数据源
grd.DataBind();
pnlResult.Controls.Add(grd);
}

但是我的问题是gridviews将一个加在另一个之下。我希望他们并肩。我怎么做到这一点?



注:面板不是强制性的。其他任何东西都可以用在它的位置上 c $ c>将面板中的元素保存为 left 。为了在你的代码中实现这一点,在向面板添加网格之前,请执行以下操作:

  grd.Attributes.Add(class, 向左飘浮); 

其中 float-left 定义为:

  .float-left {
float:left;

$ / code>

所以你的代码看起来像:

  for(int i = 1; i  {
GridView grd = new GridView();
grd.ID =GridView+ i.ToString();
grd.BackColor = getColor(i);
grd.Attributes.Add(class,float-left); // here
grd.DataSource = dt; //一些数据源
grd.DataBind();
pnlResult.Controls.Add(grd);
}


I am creating a page which shows some details in several gridviews. I am saying several because the number of gridviews is not constant. I am having a panel on the aspx page and adding gridviews to it dynamically.

aspx code:

<asp:Panel ID="pnlResult" runat="server"></asp:Panel>

aspx.cs code

int numOfGroups = some number here;
            for (int i = 1; i < numOfGroups + 1; i++)
            {
                GridView grd = new GridView();
                grd.ID = "GridView" + i.ToString();
                grd.BackColor = getColor(i);
                grd.DataSource = dt; // some data source
                grd.DataBind();
                pnlResult.Controls.Add(grd);
            }

But my problem is that the gridviews are adding one below the another . I want them to be side by side. How can I achieve that?

Note: Panel is not mandatory. Anything else can be used in its place

解决方案

You have to float your elements inside the panel to left. To achieve that in your code behind, before adding grid to the panel do:

grd.Attributes.Add("class", "float-left");

Where float-left class in your css is defined as:

.float-left {
    float: left;
}

So your code would look like:

for (int i = 1; i < numOfGroups + 1; i++)
{
    GridView grd = new GridView();
    grd.ID = "GridView" + i.ToString();
    grd.BackColor = getColor(i);
    grd.Attributes.Add("class", "float-left"); //here
    grd.DataSource = dt; // some data source
    grd.DataBind();
    pnlResult.Controls.Add(grd);
}

这篇关于如何动态添加gridviews使用asp.net c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 05:06