R – How to programmatically wire up a LINQ dat source in ASP.Net

asp.netlinqlinqdatasource

myGridView.DataSource = LinqDataSource works but only for select. I have edit and delete columns and when I try to use them I get errors about events not getting caught. Specifically I've seen OnRowDeleting, but I'm sure there are others that need to be wired up.

myGridView.OnRowDeleting = ??

I can't seem to find anything on the LinqDataSource that looks like what I need 🙁

edit: here is some some sample code illustrating what I'm doing.

protected virtual void OnRowDeleted(Object sender, GridViewDeletedEventArgs e)
{
    // it means the last row was deleted
    if (fieldGridView.Rows.Count == 1)
    {
        fieldGridView.DataSourceID = null;
        fieldGridView.DataSource = new[] { new FormField { Required = false } };
        fieldGridView.AutoGenerateDeleteButton = false;
        fieldGridView.AutoGenerateEditButton = false;
    }
}

protected void InsertButton_Click(object sender, CommandEventArgs e)
{
    // pull data out of footer row and insert it into the DB
    if (fieldGridView.DataSource == null || fieldGridView.DataSource.GetType() != LinqDataSource1.GetType())
    {
        fieldGridView.DataSource = LinqDataSource1;
        fieldGridView.AutoGenerateDeleteButton = true;
        fieldGridView.AutoGenerateEditButton = true;
    }
}

Also note that I've statically set the OnRowDeleting event in the markup and the error went away. However, the Linq data source is not getting set back properly. The code is updating it, it's just sticking for whatever reason, so what's happening is that when I re-enable the delete column the data source ends up still being the temp data source I assigned to it (which is just a list), so when I hit delete it was blowing up. Since I've now statically defined the callback, it gets called, but no delete happens because the data source isn't being switched back to the linq data source properly.

so in essence, my issue is that when I dynamically set the data source to the list on the last delete, the change is sticking, but when I dynamically set the data source to the linq data source on the first insert, the change is not sticking. The strangest part of it is that other changes I'm making do stick, just not the data source. I'm enabling the autogenerated edit and delete columns and that change is being reflected immediately.

Best Answer

On your linq datasource, did you remember to explicitly set the enable flags for delete and insert?

<asp:LinqDataSource ID="MyLinqDataSource" EnableDelete="true" EnableUpdate="true" EnableInsert="true" runat="server" ....