【】动态生成GridView列(每次需要绑定的列的数目不同),即动态绑定数据源时列数目不固定的绑定方式
日期:2013-05-09 | 分类:GridView | Tags:
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/flbh-logs/233317059.html
想实现这样的功能吗?在程序运行中使GridView动态生成列,而并不是将列固定写死在GridView上
写个绑定GrideView的方法
/// <summary>
/// 绑定生成GridView
/// </summary>
/// <param name="gdv">要绑定的GridView</param>
/// <param name="dtblDataSource">GridView的数据源</param>
/// <param name="strDataKey">GridView的DataKeyNames</param>
public static void GridViewBind(GridView gdv, DataTable dtblDataSource, string strDataKey)
{
gdv.Columns.Clear();
gdv.AutoGenerateColumns = false;
gdv.DataSource = dtblDataSource;
gdv.DataKeyNames = new string[] { strDataKey };
for (int i = 0; i < dtblDataSource.Columns.Count; i++) //绑定普通数据列
{
BoundField bfColumn = new BoundField();
bfColumn.DataField = dtblDataSource.Columns[i].ColumnName;
bfColumn.HeaderText = dtblDataSource.Columns[i].Caption;
gdv.Columns.Add(bfColumn);
}
gdv.Columns[1].Visible = false;
CommandField cfModify = new CommandField(); //绑定命令列
cfModify.ButtonType = ButtonType.Button;
cfModify.SelectText = "修改";
cfModify.ShowSelectButton = true;
gdv.Columns.Add(cfModify);
gdv.DataBind();
}
只要调用这个方法就可以啦!试试吧!
************************************************************************************************
转帖:http://hi.baidu.com/nirvanan/blog/item/4c9ba84a998d7ef883025c1b
文件名:list.aspx
前台代码:
<%@ Page Language="C#" CodeFile="List.aspx.cs" Inherits="manager_general_List" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="font-size: 12px;">
<form id="form1" runat="server">
<img src="../images/arrow03.gif" /><span style="font-weight: bold">
<asp:Literal ID="ltl_listName" runat="server"></asp:Literal></span><br />
<div style="clear: both; height: 20px; width: 100%;">
</div>
</form>
</body>
</html>
=======================================================================
后台代码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Drawing;
public partial class manager_general_List : System.Web.UI.Page
{
GridView gvshow = null;//定义全局变量
public static bool sign;
protected void Page_Load(object sender, EventArgs e)
{
string tbName = "";
if (Request.QueryString["t"]!=null&&Request.QueryString["t"]!="")
{
tbName=Request.QueryString["t"].ToString();
}
else
{
common.MsgBox("没有相应的内容!");//此为自定的弹出消息框函数。使用者可自选定义。或者去掉。
Response.End();
}
if (sign != null) //根据标记符号来判断,重新绑定那个控件
{
if (sign ==false)
{
string sql = getListSql(tbName);//获取要在gridview中显示的字段
if (sql=="")
{
common.MsgBox("没有相应的内容!");//此为自定的弹出消息框函数。使用者可自选定义。或者去掉。
Response.End();
}
try
{
setBind(sql,tbName);
ltl_listName.Text = common.getFieldValueFromTab("tb_showname", "ctbname", tbName, 0, "cshowname")+"列表";
}
catch (System.Exception errr)
{
common.MsgBox("没有相应的内容!");//此为自定的弹出消息框函数。使用者可自选定义。或者去掉。
Response.End();
}
}
}
}
//获取要在gridview中显示的字段的sql
public string getListSql(string tbName)
{
string sqlStr = "";
string temp = "";
DataSet ds = common.RunQuery("select * from tb_listfield where cname='" + tbName + "' order by cindex");
if (ds.Tables.Count>0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
temp += row["cfield"].ToString()+",";
}
}
if (temp!="")
{
temp = temp.Substring(0, temp.Length - 1);//去掉最后一个逗号
sqlStr = "select " + temp + " from " + tbName;
}
return sqlStr;
}
public void setBind(string sql,string tbName)
{
DataTable dt = getDataTable(sql); //获得数据源
string headerText = "";
gvshow = new GridView();//new一个grideview