简体   繁体   English

数据绑定不刷新 Gridview

[英]Databind not refreshing Gridview

I have a gridview that I databind dynamically in my codebehind.我有一个 gridview,我在我的代码隐藏中动态地进行数据绑定。 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).出于某种原因,并且仅当用户从 gridview 中删除一行时(使用自定义函数),在回发之后,gridview 不会刷新(删除的值仍然存在)。 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.但是,如果用户以任何其他方式(即添加项目、在另一个选项卡中选择正确的项目等)刷新 gridview,则绑定得很好。

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.我在代码隐藏中放置了断点以查看发生了什么,显然 gridview 绑定正确,而被删除的项目不在 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): bindGridView方法(每隔一次调用一次,所以我认为问题不在这里):

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 show the changes to the user. [编辑] 有趣的是,它不仅运行,而且当我在运行后检查数据源的内容时(在调试时),实际上删除的项目已从数据源中删除,它只是不显示对用户的更改。

I figured out the problem:我解决了这个问题:

Gridviews have predefined commands set up, such as 'select', 'edit', and 'delete'. Gridview 设置了预定义的命令,例如“选择”、“编辑”和“删除”。 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.我将“删除”作为 asp:ImageButton 的 CommandName 传递。 After I saw your post I changed it to pass "DeleteThis" and it began working immediately.看到您的帖子后,我将其更改为通过“DeleteThis”并立即开始工作。 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? bindgridview 是否确定运行? Have you run it in debug mode to verify?你有没有在调试模式下运行它来验证?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM