Results 1 to 2 of 2

Thread: Datagridview update help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Location
    N.Ireland
    Posts
    71

    Datagridview update help

    Hi everyone, I'm working on my first winforms EDM project and I'm trying to populate a Datagridview with a list of Presenters that can be updated by a user. The cols in my grid are Name and Email address(DataGridViewLinkColumn). So far I have been able to display the grid correctly but when I try to add a new row my changes are not being saved back to the DB even though I get a msgbox to say they have. Here's how I'm populating my DGV:
    C# Code:
    1. private void frmPresenters_Load(object sender, EventArgs e)
    2. {
    3.   dal = new DataAccessLayer();
    4.  
    5.   try
    6.   {
    7.     //Get the List of Contacts from the DAL and bind to the DataGrid
    8.     source = new BindingSource();
    9.     source.DataSource = dal.GetPresentersList();
    10.  
    11.    this.dgvPresenters.DataSource = source;
    12.    dgvPresenters.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
    13.    dgvPresenters.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    14.   }
    15.   catch (Exception ex)
    16.   {
    17.      MessageBox.Show(string.Format("Exception occurred: {0}", ex.Message));
    18.   }
    19. }
    I'm unable to edit the Email address field as well (i think this may be a seperate thread)

    all updates are made by clicking a button:
    C# Code:
    1. private void btnSave_Click(object sender, EventArgs e)
    2. {
    3.    try
    4.    {
    5.       // Save object changes to the database, display a message, and refresh the form.
    6.       this.source.EndEdit();
    7.       //dal.entities.SaveChanges();
    8.       MessageBox.Show("Changes saved to the database.");
    9.       this.Refresh();
    10.    }
    11.    catch (Exception ex)
    12.    {
    13.       MessageBox.Show(ex.Message);
    14.    }
    15. }
    Can anyone help me please?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview update help

    You see the message because you're displaying the message. Your changes don't get saved because you're not saving them. You've commented out the SaveChanges call, which is presumably what saves the changes. If you are saying, without actually saying, that it still doesn't save even when that line is not commented out then we can't really help because we don't know anything about your DataAccessLayer class. How you save data depends what technology you're using inside that class. If, for instance, you're using ADO.NET then you need to call Update on the appropriate DataAdapter or TableAdapter and pass the DataTable containing the changes.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width