当前位置:编程学习 > C#/ASP.NET >>

GridView如何能够根据不同的条件进行查询!~!~


GridView1已经绑定数据源,可以显示出全部数据。
请问如何根据不同的条件,比如:按姓名,地址等!~!~在GridView1显示不同的查询结果!~!~ --------------------编程问答-------------------- GridView本身不会查询数据,它只是负责数据的列表展示。
你需要根据条件动态取得数据,然后把数据绑定到GridView上。 --------------------编程问答-------------------- DataTable.Select

or

DataView.RowFilter --------------------编程问答-------------------- string sql = "SELECT * FROM [mzgl_knsb] where cj='车间'";
        this.SqlDataSource1.SelectCommand = sql;
        this.GridView1.DataBind();
我用这样的方式可以改变显示内容,可一换页又显示全部内容了,请问该如何实现!~! --------------------编程问答-------------------- string sql="";条件查询
或DataView dv=dt.DefaultView;
dv.RowFilter="";
--------------------编程问答-------------------- 分页时根据条件重新绑定数据
条件怎么记录的
--------------------编程问答-------------------- 你需要用到页面传值 --------------------编程问答-------------------- gridview本身不行。你需要重新绑定根据查询所获得的DataSet,然后重新绑定。 --------------------编程问答-------------------- gridview本身不行。你需要重新绑定根据查询所获得的DataSet,然后重新绑定。


需要查询后的表绑定就可以了。 --------------------编程问答--------------------

 string name = TextBox1.Text.ToString();//获取前台页面的条件值 
 string sql = "selcet * from table where name='"+ name +"'";//更新条件
 conn = new SqlConnection(connstring);//连接
        da = new SqlDataAdapter(sql, conn);
        ds = new DataSet();
        da.Fill(ds);
       
 this.MyGridView.DataSource = ds;
 this.MyGridView.DataBind();
这样你就可以获得新的数据。。。。 --------------------编程问答--------------------

public DataTable GetTable(string name,string address)
{
StringBuilder str = new StringBuilder();
str.Append(@"select * from tableName where 1=1");

if(!string.IsNullOrEmpty(name))
{
    str.AppendFormat(@" and Name='{0}'",name);
}
if(!string.IsNullOrEmpty(address))
{
    str.AppendFormat(@" and Address='{0}'",address);
}
........

return DataTable;
}

 if (!Page.IsPostBack)
        {
GetValue();
}

void GetValue()
{
 string Name=Name.Text;
 stirng Address=Address.Text;
 GridView.DataSource = GetTable(Name,Address);
 GridView.DataBind();

}

/// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
{
      GetValue();
} --------------------编程问答-------------------- 顶楼上的。。。。 --------------------编程问答-------------------- 可以把搜索条件放到一个数组里,之后在数据访问层根据该数组里的信息来进行sql语句的拼接



/// </summary>
        /// <param name="key">数组用来存储搜索的条件</param>
        /// <param name="pageSize">每页几条数据</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns></returns>
        public DataTable ManageFinance_usual(string[] key, int pageSize, int pageIndex)
        {
            sqlsterm = "1=1";
            if (key[1] != "" && key[1] != null)
                sqlsterm += " and FaTime>='" + DateTime.Parse(key[1].ToString()) + "'";
            if (key[2] != "" && key[2] != null)
                sqlsterm += " and FaTime<='" + DateTime.Parse(key[2].ToString()) + "'";
            if (key[3] != "" && key[3] != null)
                sqlsterm += " and CredenceSn like '%" + key[3].ToString() + "%'";
            if (key[4] != "" && key[4] != null)
                sqlsterm += " and FinanceContent like '%" + key[4].ToString() + "%'";
            if (key[5] != "" && key[5] != null)
                sqlsterm += " and DefrayDate>='" + DateTime.Parse(key[5].ToString())+"'";
            if (key[6] != "" && key[6] != null)
                sqlsterm += " and DefrayDate<='" + DateTime.Parse(key[6].ToString()) + "'";
            if (key[7] != "" && key[7] != null)
                sqlsterm += " and BeiZhu like '%" + key[7].ToString() + "%'";
            SqlParameter[] cmdParms = PageParms("Finance_usual", "FinanceID", "FinanceID DESC", pageSize.ToString(), pageIndex.ToString(), "*", sqlsterm, "");
            return objsqlhelper.ExecuteDataTable("LingShangOA_SP_Pagination", CommandType.StoredProcedure, cmdParms);
        }
--------------------编程问答-------------------- 用个分页查询的存储过程吧。。。。那个东西可以把你只是查询的
GridView1省力到极致。。。。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,