1

好的,所以我尝试了一些事情并一直卡住。我曾经让更新按钮工作,现在它不会更新。删除按钮将起作用并删除记录,但在删除记录后我无法刷新网格视图。我还希望在按下更新按钮并更新记录后重新加载网格视图。这是我所拥有的:

protected void btnDelete_Click(object sender, EventArgs e)
        {
            switch (btnDelete.Text)
            {
                case DeleteButton:
                    try
                    {
                        if (txtLocationName.Text != null && txtSubAccountName.Text != null)
                        {
                            Location locationCheck = _context.Locations.ToList()
                                                    .First(x => x.Name == txtLocationName.Text && x.SubAccount == txtSubAccountName.Text);
                            if (locationCheck != null)
                            {
                                Location n = new Location
                                {
                                    Id = grdvwLocationList.SelectedIndex,
                                    Name = txtLocationName.Text,
                                    SubAccount = txtSubAccountName.Text
                                };

                                _context.Locations.Remove(n);
                                _context.SaveChanges();



                            }
                        }
                    }
                    catch (Exception)
                    {
                        lblLocationNameNotification.Text = "Please type in a location/sub-account or select a location/sub-account that doesn't have a asset to delete.";
                        txtLocationName.Text = "";
                        txtSubAccountName.Text = "";
                    }
                    break;
                case CancelButton:
                    Reload();
                    break;
            }
        }





    public void PopulateLocationGridView()
    {
        var locations = _context.Locations.Where(l => l.CompanyId == CompanyId)
                        .OrderBy(l => l.Name)
                        .ToList();

        grdvwLocationList.DataSource = locations;
        grdvwLocationList.DataBind();

        if (locations.Count > 0)
        {
            grdvwLocationList.SelectedIndex = 0;
            RowSelected();
        }
        else
        {
            txtLocationName.Text = "";
            txtSubAccountName.Text = "";
        }
    }

添加按钮工作得很好,它似乎正在刷新网格视图

4

2 回答 2

1

我在 winforms 应用程序中有以下示例 诀窍是 dset.local

    private void Form1_Load(object sender, EventArgs e)
    {
        var dset = Db.Tasks;   // Db is my context.
        DbSet<Task> qry = dset;
        qry.Load();
        bindingSource1.DataSource  =dset.Local.ToBindingList();

    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        Debug.Print(Db.Tasks.Count().ToString());
        bindingSource1.EndEdit();

        Db.SaveChanges();
    }
于 2013-08-16T02:32:25.160 回答
0

昨晚工作后,我知道我缺少一些东西。我最终是这样处理的:

protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            _context = new IMSDBContext();

            switch (btnDelete.Text)
            {
                case DeleteButton:
                    if (txtLocationName.Text != null && txtSubAccountName.Text != null)
                    {
                        Location location = _context.Locations.ToList()
                                            .First(x => x.Name == txtLocationName.Text && x.SubAccount == txtSubAccountName.Text);

                        _context.Locations.Remove(location);
                        _context.SaveChanges();

                        PopulateLocationGridView();
                        grdvwLocationList.SelectedIndex = 0;
                        RowSelected();
                    }
                    break;

                case CancelButton:
                    Reload();
                    break;
            }
        }
        catch (Exception ex)
        {
            lblLocationNameNotification.Text = ex.Message;
        }
        finally
        {
            if (_context != null)
            {
                _context.Dispose();
            }
        }
    }

我曾尝试自己使用 PopulateLocationGridView() 和 RowSelect() 方法,但仍然遇到问题。我最终输入了 grdvwLocationList.SelectedIndex = 0; in 在列表中的第一个索引上设置选定的索引,而不是我刚刚删除的记录的索引。那是我遇到麻烦的地方。我认为 SelectRow() 会再次重新选择索引,但我不得不将其重置回另一个索引。如果有任何问题或意见,请随时。我仍在学习,并希望得到我能得到的所有建议。

于 2013-08-16T11:56:17.650 回答