我需要过滤一个从表中检索过滤数据的gridview。因此,我将gridview绑定到数据集。现在我似乎无法找到进一步过滤它的解决方案。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
SqlConnection myCon = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(cmd, myCon);
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//need to insert code here for filtering GridView1 based on TextBox1.Text
}
感谢您的帮助。
答案 0 :(得分:2)
试试这个:
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection myCon = new SqlConnection(connectionstring);
SqlDataAdapter adapter = new SqlDataAdapter(cmd, myCon);
adapter.Fill(ds);
DataView view = new DataView();
view.Table = ds.Tables[0];
view.RowFilter = "ColumnName = " + TextBox1.Text.Trim();
GridView1.DataSource = view;
GridView1.DataBind();
}
答案 1 :(得分:1)
这是一个完整的示例,它处理GridView的paging
,sorting
(两个方向)和filtering
(两列)。
// store sorting across postbacks in a ViewState variable
public string SortExpression
{
get
{
if (ViewState["GridSort"]== null)
{
ViewState["GridSort"] = "Column1 ASC";
}
return ViewState["GridSort"].ToString();
}
set { ViewState["GridSort"] = value; }
}
protected void Page_load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
这是完成所有操作的主要方法(包括filter
- 函数):
private void BindGrid()
{
try
{
var tblData = new DataTable();
var filter1 = TextBox1.Text.Trim();
var filter2 = TextBox2.Text.Trim();
using (var sqlCon = new System.Data.SqlClient.SqlConnection(connectionstring))
{
String sql = String.Empty;
var sqlCmd = new System.Data.SqlClient.SqlCommand();
if (filter1.Length != 0 && filter2.Length != 0)
{
sql = "SELECT Column1,Column2 FROM Table WHERE Column1 LIKE @Column1 AND Column2 LIKE @Column2 ORDER BY {0}";
sqlCmd.Parameters.AddWithValue("@Column1", string.Format("%{0}%", filter1));
sqlCmd.Parameters.AddWithValue("@Column2", string.Format("%{0}%", filter2));
}
else if (filter1.Length != 0)
{
sql = "SELECT Column1,Column2 FROM Table WHERE Column1 LIKE @Column1 ORDER BY {0}";
sqlCmd.Parameters.AddWithValue("@Column1", string.Format("%{0}%", filter1));
}
else if (filter2.Length != 0)
{
sql = "SELECT Column1,Column2 FROM Table WHERE Column2 LIKE @Column2 ORDER BY {0}";
sqlCmd.Parameters.AddWithValue("@Column2", string.Format("%{0}%", filter2));
}
else
{
// no filter, select all
sql = "SELECT Column1,Column2 FROM Table ORDER BY {0}";
}
sqlCmd.CommandText = string.Format(sql, this.SortExpression);
sqlCmd.Connection = sqlCon;
using (System.Data.SqlClient.SqlDataAdapter objAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd))
{
objAdapter.Fill(tblData);
}
}
GridView1.DataSource = tblData;
GridView1.DataBind();
}
catch (Exception)
{
// log
throw;
}
}
Paging
:
private void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}
过滤-扣点击:
private void BtnFilter_Click(object sender, System.EventArgs e)
{
BindGrid();
}
Sorting
:
protected void GridView1_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
string currentSortColumn = null;
string currentSortDirection = null;
currentSortColumn = this.SortExpression.Split(' ')[0];
currentSortDirection = this.SortExpression.Split(' ')[1];
if (e.SortExpression.Equals(currentSortColumn))
{
//switch sort direction
switch (currentSortDirection.ToUpper())
{
case "ASC":
this.SortExpression = currentSortColumn + " DESC";
break;
case "DESC":
this.SortExpression = currentSortColumn + " ASC";
break;
}
}
else
{
this.SortExpression = e.SortExpression + " ASC";
}
BindGrid();
}
刚从VB手动转换,所以我希望没有剩余的错误。
答案 2 :(得分:0)
sql = new SqlConnection(Connection.con);
adapter = new SqlDataAdapter(@"select EntryID * from Table where Name like @Name ", sql);
adapter.SelectCommand.Parameters.AddWithValue("@Name", string.Format("%{0}%", textBox1.Text));
dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;