ASP.NET中gridview根据条件查询将数据库的数据读出来

kelly_water 2013-05-08 05:32:27
在设计页面中将gridview绑定数据源,打开页面的时候可以显示数据源里面的数据。但是想在页面中多一个条件查询,点击查询将查询到的数据重新显示在gridview中,gridview将数据库的数据读出来,后台代码该如何实现。
protected void find_onclick(object sender, EventArgs e)
{

String findcondition = conditionfind.Text.Trim();
String stucondit = stucondition.Value.Trim();
String condit=null;
if (findcondition == "学号")
{
condit = "sno";
}
if (findcondition == "姓名")
{
condit = "sname";
}
if (findcondition == "专业")
{
condit = "mname";
}

SqlConnection conn = new SqlConnection(connString);

String sql = String.Format("select * from v_findStudent where '{0}'='{1}'", condit, stucondit);
try
{
SqlDataAdapter dataad = new SqlDataAdapter(sql, conn);
DataSet dadaset = new DataSet("v_findStudent");
dataad.Fill(dadaset);

this.GridView1.DataSourceID = null;
GridView1.DataSource = dadaset.Tables[0];
GridView1.DataBind();


}


}
...全文
1018 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2013-05-09
  • 打赏
  • 举报
回复
String sql = String.Format("select * from v_findStudent where {0}='{1}'", condit, stucondit); 不要单引号试试
liu_lxx 2013-05-09
  • 打赏
  • 举报
回复
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            string sql_str = "SELECT GS_ID,GSMC,GSYX,GSQZ,LXR,LXDH,GDDH,BZ FROM YH_GS";
            DataBind(sql_str);
        }
    }   
 protected void Bt_search_Click(object sender, EventArgs e)
    {
        string str_search = Txt_search.Text;
        string sql_str = "SELECT GS_ID,GSMC,GSYX,GSQZ,LXR,LXDH,GDDH,BZ FROM YH_GS WHERE GSYX LIKE '%" + str_search + "%' OR GSMC LIKE '%" + str_search + "%' OR GSQZ LIKE '%" + str_search + "%' OR LXR LIKE '%" + str_search + "%' OR LXDH LIKE '%" + str_search + "%'";
        DataBind(sql_str);
    }   
 private void DataBind(string sql_str)
    {
        DataSet ds_gsinfo = WebFunction.dataSet(sql_str);
        Gv_gsinfo.DataSource = ds_gsinfo;
        Gv_gsinfo.DataBind();
    }
踏平扶桑 2013-05-09
  • 打赏
  • 举报
回复

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ViewState["SortOrder"] = "Num";
            ViewState["OrderDire"] = "ASC";
            binddata();
        }
    }

    private void binddata()
    {
        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        if (Request["pos"] == null && Request["num"] == null && Request["bh"] == null)
        {
            //list
            GridView1.DataSource = db1.search("", sort);

            GridView1.DataBind();
        }
        else
        {//条件查询
            if (Request["pos"] != null)
            {
                GridView1.DataSource = db1.search("POS like '%" + Request["pos"].ToString() + "%'", sort);
            }
            if (Request["num"] != null)
            {
                GridView1.DataSource = db1.search("num like '%" + Request["num"].ToString() + "%'", sort);
            }
            if (Request["bh"] != null)
            {
                GridView1.DataSource = db1.search("bh like '%" + Request["bh"].ToString() + "%'", sort);
            }
            GridView1.DataBind();
        }
    }
bidisty 2013-05-09
  • 打赏
  • 举报
回复
String sql = String.Format("select * from v_findStudent") ………… var dt=dadaset.Tables[0]; if(string.IsNullOrEmpty(stucondit)) { dt.Select(w=>w.学号.Contains(stucondit )) } GridView1.DataSource = dt; GridView1.DataBind(); 大概就是这个意思吧,此方法有性能问题。
kelly_water 2013-05-09
  • 打赏
  • 举报
回复
引用 6 楼 Chinajiyong 的回复:
String sql = String.Format("select * from v_findStudent where {0}='{1}'", condit, stucondit); 不要单引号试试
谢谢,正解,可以了
kelly_water 2013-05-09
  • 打赏
  • 举报
回复
引用 3 楼 Kim_Du 的回复:
你这不是都已经写出来了吗?有什么问题?
没有办法查询出来
kelly_water 2013-05-09
  • 打赏
  • 举报
回复
我原本那样写点击查询没有办法把符合条件的查询出来,但是觉得逻辑是没有问题的,gridview有没有可以把数据库里的数据一列一列的读出来的代码,类似于form里面的dataGridView1.Columns[0].DataPropertyName = "startyear";startyear是字段名
Kim_Du 2013-05-08
  • 打赏
  • 举报
回复
你这不是都已经写出来了吗?有什么问题?
md5e 2013-05-08
  • 打赏
  • 举报
回复
string str = ""; if (Request.QueryString["bclassid"] != null && Request.QueryString["sclassid"] != null) { str = " and sclassid=" + Request.QueryString["sclassid"]; } if (!IsAdmin()) { //String Legal = Request.QueryString["Legal"]; //if (Legal != "all") //{ // if (!CheckLegal(Legal, Session["MLegal"].ToString())) // Response.Redirect("Legal.aspx"); //} str = str + " and UserName='" + Session["MUserName"] + "'"; //btn_Ok.Enabled = false; } string strqry = ""; if (txt_Search.Text == "") strqry = "select * From V_NewsSystem where id>0 " + str + " order by FBDate desc,id desc"; else strqry = "Select * From V_NewsSystem where title like '%" + txt_Search.Text + "%' " + str + " order by FBDate desc,id desc"; 就是普通的拼接
kelly_water 2013-05-08
  • 打赏
  • 举报
回复
新手上路,求解

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧