gridview动态绑定问题 gridview里不是每一列都需要绑定,比如a,b,c列需要绑定,d,e列不需要绑定,而查询得的数据源datatable也不是每一列都可以用来绑定,比如a,b,c列需要用于绑定,f,g列不需要,请问该怎么实现动态绑定? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在boundField或模板列里设置字段名称也可在GridView1_RowCreated设置表头。 直接写就可以了啊.不需要用控件,自己抓Dataset,再绑定. boundfield你要什么列就什么列 花了一些时间写了点东西,不知是不是LZ要的效果? 类文件 BaseClass.cs /// <summary> /// 说明:GetDataSet数据集,返回数据源的数据集 /// 返回值:数据集DataSet /// 参数:sQueryString SQL字符串,TableName 数据表名称 /// </summary> public System.Data.DataSet GetDataSet(string sQueryString, string TableName) { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); con.Open(); SqlDataAdapter dbAdapter = new SqlDataAdapter(sQueryString, con); DataSet dataset = new DataSet(); dbAdapter.Fill(dataset, TableName); con.Close(); return dataset; }----------------------------------------------------------------------Test.aspx前台:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"> </asp:GridView>//注意AutoGenerateColumns="true"Test.aspx.cs后台: BaseClass bc = new BaseClass(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = bc.GetDataSet("select * from [table]", "table"); //table为你的表名 GridView1.DataBind(); } }GridView1要显示哪一列,在select * from [table]这里写就行了,比如刚开始显示a,b,c列就为GridView1.DataSource = bc.GetDataSet("select a,b,c from [table]", "table");搜索时:在TextBox1中输入内容搜索时,比如要显示a,b列,可以这么写GridView1.DataSource = bc.GetDataSet("select a,b from [table] where a like '%"+TextBox1.Text+"%'", "table"); 基本上是这样,如果需要引用不显示的列,可以用模板列+隐藏控件,或者DatakeyName 点击DRIDVIEW-->编辑列-->左下角那个自动生成字段去掉-->添加你需要的(BoundField),如果还有别的操作就用模板列-->点击这个BoundField在右边的数据栏的DataField属性中填写你数据库中需要的字段名 如何让ajaxpro和ashx共存! 请问一个C#语句转换成VB怎么写? 关于datagrid查询获得数据进行更新的问题!!!! access做新闻模块求思路 一个很简单的问题 如何保证控件style="display:none;"时内容不被复制? 请问,如何在DataGrid中的第一列实现自动编号,但是,必须是按照倒序自动编号??急! 换行问题! 请教思归!!asp.net中弹出对话框的问题 如何在后台cs文件里动态生成DataGrid并绑定 基于.net 3.5的网站无法运行 [玻璃鱼V]自学.NET中碰到的几个问题~
也可在GridView1_RowCreated设置表头。
不需要用控件,自己抓Dataset,再绑定.
你要什么列就什么列
/// 说明:GetDataSet数据集,返回数据源的数据集
/// 返回值:数据集DataSet
/// 参数:sQueryString SQL字符串,TableName 数据表名称
/// </summary>
public System.Data.DataSet GetDataSet(string sQueryString, string TableName)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
con.Open();
SqlDataAdapter dbAdapter = new SqlDataAdapter(sQueryString, con);
DataSet dataset = new DataSet();
dbAdapter.Fill(dataset, TableName);
con.Close();
return dataset;
}----------------------------------------------------------------------
Test.aspx
前台:<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"> </asp:GridView>
//注意AutoGenerateColumns="true"Test.aspx.cs
后台:
BaseClass bc = new BaseClass(); protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = bc.GetDataSet("select * from [table]", "table"); //table为你的表名
GridView1.DataBind();
}
}
GridView1要显示哪一列,在select * from [table]这里写就行了,比如刚开始显示a,b,c列就为
GridView1.DataSource = bc.GetDataSet("select a,b,c from [table]", "table");搜索时:
在TextBox1中输入内容搜索时,比如要显示a,b列,可以这么写
GridView1.DataSource = bc.GetDataSet("select a,b from [table] where a like '%"+TextBox1.Text+"%'", "table");
-->编辑列
-->左下角那个自动生成字段去掉
-->添加你需要的(BoundField),如果还有别的操作就用模板列
-->点击这个BoundField在右边的数据栏的DataField属性中填写你数据库中需要的字段名