如何在asp.net中动态添加gridview c# [英] How to dynamically add gridview in asp.net c#

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

问题描述

你好,我必须在asp.net中动态添加多个gridview。没有gridview是基于选择而生成的。
先进的感谢

解决方案

如果我没有理解错误标题,动态添加多个网格视图意味着要添加GridView是ASP.NET C#中的一个类,我们可以创建它的对象并设置其属性,就像其他类对象一样如下所示:

  GridView objGV = new GridView(); 
objGV .AutoGenerateColumns = false;

可以从代码中添加像BoundField和TemplateField这样不同类型的列如下所示:

  BoundField field = new BoundField(); 
field.HeaderText =列标题;
field.DataField = Value;
objGV .Columns.Add(field);

最后可以在任何容器控件(如面板)下的.aspx中添加此网格视图对象。

  PanelId.Controls.Add(objGV); 

要添加多个网格实例,只需循环上面的代码即可:

  for(int i = 0; i< yourConditionCount; i ++)
{
GridView objGV = new GridView();
objGV.ID =GV+ i; //每个网格视图的ID必须是唯一的

//您的代码逻辑为网格视图设置属性和事件

PanelId.Controls.Add(objGV);
}

希望我能正确理解您的要求,我的解释对您有所帮助。 / p>

Hello guys i have to dynamically add multiple gridview in asp.net. There are no of gridview are genereated on the basis of selection. Thanks in advanced

解决方案

If I have not understood wrong from title that add multiple grid view dynamically means want to add grid view from code behind at run time.

As GridView is a class in ASP.NET C# and we can create object of it and set its properties just like other class object like follows:

GridView objGV = new GridView();
objGV .AutoGenerateColumns = false;

and can add columns of different type like BoundField and TemplateField from code Like follows:

BoundField field = new BoundField();
field.HeaderText = "Column Header";
field.DataField = Value;
objGV .Columns.Add(field);

and finally can add this grid view object on .aspx under any container control like panel.

PanelId.Controls.Add(objGV );

For adding multiple grid instance just iterate above code in loop like:

for(int i=0;i<yourConditionCount;i++)
{
    GridView objGV = new GridView();
     objGV.ID="GV"+i;   // ID of each grid view must be unique

    // your code logic to set properties and events for grid view

   PanelId.Controls.Add(objGV );
} 

Hope I understood your requirement correctly and my explanation will be helpful for you.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆