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

888 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

Ultimately I was not able to fix this specific issue, but I did find a workaround for my larger issue.

In essence what I've done is to create two gridviews with different data sources, created a custom delete button for each row, and then swap the grids out accordingly. Not exactly a great solution, but it suffices for what I'm doing.

As for why my original solution of swapping the datasources doesn't work, I don't know. Most of the projects I've been on have used non-MS tools for tabulated data so I'm not even sure where to look. However, if anyone has any good ideas as to why I've encountered this behavior, I'm all ears.

7
On

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" ....