gridview里不是每一列都需要绑定,比如a,b,c列需要绑定,d,e列不需要绑定,而查询得的数据源datatable也不是每一列都可以用来绑定,比如a,b,c列需要用于绑定,f,g列不需要,请问该怎么实现动态绑定?

解决方案 »

  1.   

    在boundField或模板列里设置字段名称
    也可在GridView1_RowCreated设置表头。
      

  2.   

    直接写就可以了啊.
    不需要用控件,自己抓Dataset,再绑定.
      

  3.   

    boundfield
    你要什么列就什么列
      

  4.   

    花了一些时间写了点东西,不知是不是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"); 
      

  5.   

    基本上是这样,如果需要引用不显示的列,可以用模板列+隐藏控件,或者DatakeyName
      

  6.   

    点击DRIDVIEW
    -->编辑列
    -->左下角那个自动生成字段去掉
    -->添加你需要的(BoundField),如果还有别的操作就用模板列
    -->点击这个BoundField在右边的数据栏的DataField属性中填写你数据库中需要的字段名