1

我需要过滤从表中检索过滤数据的网格视图。因此,我将 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        
}

谢谢您的帮助。

4

3 回答 3

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();
}
  • 你必须重构你的代码。
于 2012-06-05T08:29:15.080 回答
2

这是处理 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-function):

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手动转换,所以我希望没有剩余的错误。

于 2012-06-05T08:59:41.627 回答
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;
于 2012-06-05T08:28:55.377 回答