开发者

Databind not refreshing Gridview

开发者 https://www.devze.com 2023-03-17 22:18 出处:网络
I have a gridview that I databind dynamically in my codebehind. For some reason, and only when a user deletes a row from the gridview (using a custom function), after postback, the gridview isn\'t ref

I have a gridview that I databind dynamically in my codebehind. For some reason, and only when a user deletes a row from the gridview (using a custom function), after postback, the gridview isn't refreshed (the deleted value remains). However, if the user were to refresh the gridview in any other way (ie. adding an item, selecting the right item in another tab, etc), it is bound just fine.

I put breakpoints in my codebehind to see what was happening, and apparently the gridview is being bound correctly, and the item that was deleted isn't in it's gridview.DataSource.

Here is my code, so if you see what I don't, just let me know!

Delete Row-Command:

if (e.CommandName == "delete")
        {
            int selectedId = int.Parse(e.CommandArgument.ToString());
            //delete selected row from database
            var item = (Item)DataContext.Items.Where(item => item.ItemId == selectedId).Single();
            if (item != null)
            {
                DataContext.CompanyGoalPrograms.DeleteObject(item);
            }
            DataContext.SaveChanges();

            bindGridView(currentId); //firing, but not refreshing gv after postback
            // currentId is a static variable
        }

bindGridView method (works every other time its called, so I don't think the problem is here):

protected void bindGridView(long thisId)
    {
        var query = from items in DataContext.Items
                    where items.SubSomething.Something.SomethingId == thisId && goals.SubSomething.YearId == selectedYearId //<--another static variable
                    select items;
        Gridview1.DataSource = from items in query.AsEnumerable()
                                    select new
                                    {
                                        items.Field1,
                                        items.Field2,
                                        items.Field3,
                                        Field4 = ((decimal)items.Field4).ToString("N2"),
                                        Field5 = ((decimal)items.Field5).ToString("N2"),
                                        Field6 = String.Format("{0:#,##0}", (long)items.Field6),
                                        Field7 = items.Field4 == null ? "$0.00" : ((decimal)items.Field7).ToString("C"),
                                    };
        Gridview1.DataBind();
     }

[EDIT] The funny thing is, not only does it run, but when I check the contents of the datasource after its been run (whilst debugging), the item that was deleted actually is removed from the datasource, it just doesn't s开发者_如何学Gohow the changes to the user.


I figured out the problem:

Gridviews have predefined commands set up, such as 'select', 'edit', and 'delete'. When I try to define my own event to run with the command name 'delete', the program will try and run parts of my event alongside it's predefined event. This is what has been causing issues.


That was totally the problem with mine as well. I was passing "Delete" as the CommandName for the asp:ImageButton. After I saw your post I changed it to pass "DeleteThis" and it began working immediately. Reserved word problems like these....!

Thanks for posting the solution after the fact. It helped at least one person.

Victor Del Prete


Does the bindgridview get ran for sure? Have you run it in debug mode to verify?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号