using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.Linq; namespace LINQtoSQL { public partial class _Default : System.Web.UI.Page { //Get customers' collection from DataContext NWindDataContext context = new NWindDataContext("Data Source=.;Initial Catalog=Northwind;Integrated Security=TRUE"); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) BindData(); } private void BindData() { //Select customer's from USA with LINQ query var custs = from p in context.Customers where p.Country == "USA" select new { p.CustomerID, p.ContactName, p.CompanyName, p.Country }; //Bind filtered collection to gridview this.gvCustomers.PageSize = 5; this.gvCustomers.DataSource = custs; this.gvCustomers.DataBind(); } protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e) { //Get customer object from selected row var cust = context.Customers.Where(p => p.CustomerID == gvCustomers.SelectedValue.ToString()); //Bind filtered collection to detailview dvCustomers.DataSource = cust; dvCustomers.DataBind(); } } }