日期:2014-05-17  浏览次数:20457 次

gridview控件自动排寻后,按某一条件查询时报错:数据源不支持派寻
页面源码

GridView的 AllowPaging="True" AllowSorting="True" 

<asp:BoundField DataField="name" HeaderText="楼宇名称" SortExpression="name" />
<asp:BoundField DataField="manager" HeaderText="管理员" SortExpression="manager" />
<asp:BoundField DataField="tel" HeaderText="联系电话" SortExpression="tel" />
<asp:BoundField Visible="False" DataField="bid" HeaderText="bid" InsertVisible="False"
  ReadOnly="True" SortExpression="bid" />

在程序运行时若点击某一列的标题后能排序,但是排序后再按某一条件查询时就会出错,查询代码:
  string strcn = ConfigurationManager.ConnectionStrings["mycon"].ToString();
  SqlConnection cn = new SqlConnection(strcn);
  cn.Open();
  string strsql = "select bid,name,manager,tel from building";
  if (txtcxgly.Text != "")
  {
  strsql = strsql+" where manager='" + txtcxgly.Text+"'";
  }
  if (ddllm.SelectedIndex != 0)
  {
  strsql = strsql+" where bid="+ddllm.SelectedValue;
  }
  if (ddllm.SelectedIndex != 0 && txtcxgly.Text != "")
  {
  strsql = strsql+" where manager='" + txtcxgly.Text+ "' and bid=" + ddllm.SelectedValue;
  }
  SqlDataAdapter adp = new SqlDataAdapter(strsql, cn);
  DataSet ds = new DataSet();
  adp.Fill(ds, "query");
  GridView1.DataSourceID = null;
  GridView1.DataSource = ds.Tables[0].DefaultView;
  GridView1.DataBind();这句出错,提示数据源不支持排序  
  cn.Close();

------解决方案--------------------
GridView1.DataSource = ds.Tables[0].DefaultView;
改成GridView1.DataSource = ds;