Custom DataContext and Query with a Gridview bound to a LinqDataSource

Using an editable grid with a LinqDatasource with a custom connection string and query

Binding to a custom datasource

Create a custom datacontext object connected to the database to be used;

private string _database;
private SomeDataContext _dc;
protected void Page_Load(object sender, EventArgs e)
{
    _database = Common.GetRequestConnectionString();
    _dc = new SomeDataContext(_database);
}

Catch the contextcreating event on the datasource, and ovveride the ObjectInstance:

protected void LinqDataSource1_ContextCreating(object sender, LinqDataSourceContextEventArgs e)
{
    e.ObjectInstance = _dc;
}

Setting a custom query

Catch the Selecting event on the datasource to set a custom query:

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    string keyword = TextBoxTrefwoord.Text.ToLower();
    if (string.IsNullOrEmpty(keyword)) keyword = "999999";
    e.Result = from i18n in sometable
               where i18n.Term_nl.ToLower().IndexOf(keyword) != -1
               select i18n;
 
}

And to trigger the selection, use a button to have the gridview perform a databind.

protected void ButtonFilter_Click(object sender, EventArgs e)
{
    GridView1.DataBind();
}