button、dropdownlist、textbox查询数据并将结果显示到gridview中,为什么按条件查询后不显示查询接过 20
以下是cs代码publicpartialclassadmin_studentOperation_infostu:System.Web.UI.Page{protectedv...
以下是cs代码
public partial class admin_studentOperation_infostu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind1();
}
}
public void bind1()//绑定数据
{
string a = TextBox1.Text.Trim().ToString();
string b = DropDownList1.SelectedValue.ToString();
string sqlstr = "";
if (a == "")
{
sqlstr = "select * from student order by stuID";
}
else
{
switch (b)
{
case "0":
sqlstr = "select * from student where stuID like '" + a + "% ' ";
break;
case "1":
sqlstr = "select * from student where stuname like '" + a + "% ' ";
break;
case "2":
sqlstr = "select * from student where stugrade like '" + a + "% ' ";
break;
case "3":
sqlstr = "select * from student where stuclass like '" + a + "% ' ";
break;
}
}
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open") //操作完,如果连接处于打开,则关闭连接
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)//查询
{
bind1();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)//分页
{
GridView1.PageIndex = e.NewPageIndex;
bind1();
}
} 展开
public partial class admin_studentOperation_infostu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind1();
}
}
public void bind1()//绑定数据
{
string a = TextBox1.Text.Trim().ToString();
string b = DropDownList1.SelectedValue.ToString();
string sqlstr = "";
if (a == "")
{
sqlstr = "select * from student order by stuID";
}
else
{
switch (b)
{
case "0":
sqlstr = "select * from student where stuID like '" + a + "% ' ";
break;
case "1":
sqlstr = "select * from student where stuname like '" + a + "% ' ";
break;
case "2":
sqlstr = "select * from student where stugrade like '" + a + "% ' ";
break;
case "3":
sqlstr = "select * from student where stuclass like '" + a + "% ' ";
break;
}
}
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
finally
{
if (conn.State.ToString() == "Open") //操作完,如果连接处于打开,则关闭连接
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)//查询
{
bind1();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)//分页
{
GridView1.PageIndex = e.NewPageIndex;
bind1();
}
} 展开
3个回答
展开全部
DataSet ds = new DataSet();
da.Fill(ds, "table");
GridView1.DataSource = ds.Tables["table"];
填充dataset改成这样试试? 可以直接gridview1.datasource=ds;因为你ds中就一个table
da.Fill(ds, "table");
GridView1.DataSource = ds.Tables["table"];
填充dataset改成这样试试? 可以直接gridview1.datasource=ds;因为你ds中就一个table
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
解决了吗 没解决 找我
960059842
你的case是不是有问题啊
960059842
你的case是不是有问题啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你先单步运行一下,看看DataTable中有数据不?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询