views:

283

answers:

2

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.

A: 

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" ....
Stephen M. Redd
yes, the datagrid works normally.I'm attempting to get around the issue with the gridview not rendering when there are no rows, so I'm detecting this and swapping the datasource out for a list with a single element in it. When a new item is added I swap the LINQ data source back in, but all of the events aren't wired up properly.So if the gridview starts with elements in it I can add, delete, and update normally, it's only when I delete them all, add some, and then try to delete the newly added ones that I experience this issue.
Fred
Can you post the code, or a simplified example that exhibits the same problem? Also, have you considered using the empty data template to allow the initial insert instead of manually swapping the datasource for the grid to fake it into rendering the item templates?
Stephen M. Redd
unfortunately I need the header and footer rows to render since I'm using the footer row as an insert row.
Fred
Clearly though, using the foorter for inserts when there are no existing rows has resulted in some complex and unwanted side-effects. You can add a very simple insert form to the empty items template to handle the special case, without complicating the rest of your grid for the typical case. Two simple insert mechanisms that are easy to work with and maintain beats one mechanism that is buggy and difficult to maintain.
Stephen M. Redd
In other words, you don't know how to do it.Now if we could convince the real world to wrap itself up with nice pat answers then you might be on to something, let me know when that happens.
Fred
He actually suggested a workaround and if the workaround works it works. In fact, I would take a real good look on why there aren't many examples of people switching datasources in order to get what you're trying to achieve.
Min
I got the idea from an online source.Besides that, changing the user experience because of a limitation in the web controls is an unacceptable answer.
Fred
A: 

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.

Fred