搜索GridView

时间:2014-05-05 17:54:18

标签: linq gridview entitydatasource

我正在尝试制作可搜索的GridView。它的DataSource是EntityDataSource。我有一个文本框和一个按钮。问题是我需要使用Linq来访问数据。我还没有任何代码,因为我在Linq网并且不确定我在做什么。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这应该可以帮助您入门

 protected void Page_Load(object sender, EventArgs e)
    {
        List<Customer> lstCust = new List<Customer>();
        if (!IsPostBack)
        {
            for (int i = 0; i < 10; i++)
            {
                Customer c = new Customer();
                c.FName = "FistName " + i.ToString();
                lstCust.Add(c);
            }
            Session["Data"] = lstCust;
            GridView1.DataSource = lstCust;
            GridView1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string searchText = TextBox1.Text;
        List<Customer> lstCustSearch = new List<Customer>();
        List<Customer> lstCust = new List<Customer>();
        lstCust = Session["Data"] as List<Customer>;

        lstCustSearch = (from data in lstCust
                         where data.FName.Contains(searchText)
                         select data).ToList();
        GridView1.DataSource = lstCustSearch;
        GridView1.DataBind();

    }
}

public class Customer
{
    public string FName { get; set; }
}

对不起编码约定。刚刚想出了这个样本