动态创建网格 [英] Creating grid dynamically

查看:79
本文介绍了动态创建网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户要求动态创建一个网格.

例如.我有一个文本框,如果用户输入4,则它应该是网格中的4列.


我紧急需要帮助,

在此先感谢......

I want to create a grid dynamically as per the user requirements.

EG. I have one textbox and if the user enters 4 it should be a 4 column in grid.


I need help urgently,

Thanks in Advance......

推荐答案

[ ^ ]存档项目可能帮助.
This[^] Archive item may help.


对于具有动态列的gridview,您需要像这样为网格设置一些属性...

AutoGenerateColumns="true"

所以它将根据您的数据生成列数,这意味着如果您的数据表包含4列数据,那么网格将显示4列,或者如果它包含2列数据,那么它将显示2列....

就像您的数据表是这样...

表1
学生姓名,罗尔诺,城市,部门

然后网格将包含此列..

或数据是否会是这样..

学生姓名,ROLNO

那么它将仅在网格中显示2列...


因此,您需要像这样触发选择查询.或使用此标准为网格提供数据源....

像这样的网格设计...

for gridview with dynamic column you need to set some property to your grid like this...

AutoGenerateColumns="true"

so it will genrate the number of column as per your data, means if your datatable contains 4 column data then the grid will show 4 column, or if it will contains 2 column data then it will display 2 columns....

like if your data table like this...

Table1
STUDENTNAME,ROLNO,CITY,DEPARTMENT

then the grid will contains this column..

or if the data will be like this..

STUDENTNAME,ROLNO

then it will show only 2 column in grid...


so for this you need to fire the select query like this. or provide datasource to grid with this criteria....

Grid Design like this...

<asp:gridview id="GridView1" runat="server" autogeneratecolumns="true" xmlns:asp="#unknown">
</asp:gridview>


因此,您必须创建一个ADO.NET Table,这意味着DATA TABLE的用户输入的列数是....
并向该表中添加一个或所需的空白行,并将其提供给gridviews数据源...

So you have to create a ADO.NET Table means DATA TABLE with the number of column user enter...
and add one or needed blank row to that table and give that to gridviews data source...

DataTable dt = new DataTable();
for (int i = 0; i < 4; i++)
{
    string columnName = "COLUMN" + i.ToString();
    dt.Columns.Add(columnName);
}
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
dt.AcceptChanges();
GridView1.DataSource = dt;
GridView1.DataBind();



在这里,我静态给出4,但是您可以使用变量给用户定义....



here i give 4 statically but you can give it user defined with use of variables....


这篇关于动态创建网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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