linqgridviewentitydatasource

Searching GridView


I am trying to make searchable GridView. it's DataSource is EntityDataSource. I have one textbox and a button. The problem is I need to use Linq to access the data. I don't really have any code yet, because I'm net at Linq and not sure what I'm doing. Can anyone help me?


Solution

  • This should help you to get started

     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; }
    }
    

    sorry for the coding convention. just came up with this sample