1

Ok so I have tried a few things and keep getting stuck. I had the update button working at one point and now it will not update. The delete button will work and delete records but I can not get the gridview to refresh after the record is deleted. I also want the gridview to reload after the update button is pressed and the record is updated. Here is what I have:

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 = "";
        }
    }

The add button works just fine it just seems to be refreshing the grid view

1
  • That's a heck of a lot of code for someone to read and try to find your problem. I recommend creating a very simple model and application with the bare minimum code to recreate the problem you're having. This has two benefits: 1. Debugging the simpler code is quicker and easier and what you learn can be applied to your real code easily, and 2. You'll likely get more help posting that code to stackoverflow.com. Commented Aug 15, 2013 at 17:37

2 Answers 2

1

I have the following exampe working in a winforms application The trick is the 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();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

After working on it last night I knew there was something that I was missing. I ended up going about it like this:

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();
            }
        }
    }

I had tried to use the PopulateLocationGridView() and RowSelect() methods by themselves and it was still having trouble. I ended up putting in the grdvwLocationList.SelectedIndex = 0; in to set the selected index on the first index in the list instead of the index of the record that I just deleted. That is where I was having trouble. I thought that the SelectRow() would reselect the index again but I had to reset it back to another index. If there are any questions or comments please feel free. I am still learning and would like all of the advise that I can get.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.