0

ユーザーがいくつかのフィルターを適用した後、グリッド ビューで表示されるすべてのデータを取得することに興味があります。たとえば、元のグリッドのデータソースには 10 個のレコードが含まれていますが、ユーザーがフィルターを適用した後も 5 個しか表示されていない場合、それらの 5 個を取得してリストに入れたいと考えています。これはどのように達成できますか?

4

2 に答える 2

0

Assuming your grid is bound a some collection, then on postback, you can grab the datasource of the gridview, apply your filters, and save it to a new collection.

何かのようなもの:

var datasource = yourGridView.DataSource as List<someType>;
var filteredResults = datasource.Where( ... ); // apply your filters inside the Where

明らかに、例の変数名/型をコードで使用しているものに置き換えてください。

于 2013-03-04T14:44:34.523 に答える
0

グリッド ビューをフィルター処理する組み込みの方法の、ちょっとした例を次に示します。

protected void btnSearch(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("MyConn");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();

    cmd.Connection = con;
    cmd.CommandText = "SELECT * FROM CustomerTable WHERE NumberOfCustomer = @NumberOfCustomer";
    cmd.Parameters.AddWithValue("NumberOfCustomer", TextBox1.Text);

    try
    {
        con.Open();
        sda.Fill(dt);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        con.Close();
    }

    GridView1.DataSource = dt;
    GridView1.DataBind();
}

次に、データソースをリストにバインドします。

protected void bindToList(object sender, EventArgs e)
{
    var datasource = GridView1.DataSource as List<Customers>;

    if ( !IsPostBack )
    {
        DropDownList ddl = new DropDownList();
        ddl.DataTextField = "Name";
        ddl.DataValueField = "Id";
        ddl.DataSource = datasource;
        ddl.DataBind();

        ddl.SelectedValue = list.Find( o => o.Selected == true ).Id.ToString();
    }
}
于 2013-03-04T14:56:58.360 に答える