Click here to Skip to main content
15,794,795 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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#
private void frmPresenters_Load(object sender, EventArgs e)
{
  dal = new DataAccessLayer();

  try
  {
    //Get the List of Contacts from the DAL and bind to the DataGrid
    source = new BindingSource();
    source.DataSource = dal.GetPresentersList();

   this.dgvPresenters.DataSource = source;
   dgvPresenters.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
   dgvPresenters.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
  }
  catch (Exception ex)
  {
     MessageBox.Show(string.Format("Exception occurred: {0}", ex.Message));
  }
}

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#
private void btnSave_Click(object sender, EventArgs e)
{
   try
   {
      // Save object changes to the database, display a message, and refresh the form.
      this.source.EndEdit();
      //dal.entities.SaveChanges();
      MessageBox.Show("Changes saved to the database.");
      this.Refresh();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}


Can anyone help me please?

[edit]pre tag for C# not set properly in the first code block is corrected - PES [/edit]
Posted
Updated 1-Mar-12 3:57am
v3
Comments
[no name] 29-Feb-12 15:56pm    
What is "dal.GetPresentersList()" returning?

Calling EndEdit updates the underlying datasource but does not neccessarily save changes back to the database.
pmcm 5-Mar-12 4:41am    
"dal.GetPresentersList()" was returning a List object but I've amended that to now be this:
<pre lang="c#">
public ObjectResult<Presenters> GetPresentersList()
{
// Check we have an ObjectContext
if (entities == null) entities = new CompanySecretaryEntities();
// Create a query from the entityset
var query = from p in entities.Presenters
select p;

ObjectQuery<Presenters> presenters = (ObjectQuery<Presenters>)query;

return presenters.Execute(MergeOption.AppendOnly);
}</pre>

As mentioned by Wes Aday, calling EndEdit from the DataGridView instance does not save changes to the database. To do that you can use a TableAdapter. Calling its Update method will save changes to the database.

How to insert, update, delete using DataGrid[^]

Updating data in SQL using datagridview in VS C# Windows Form[^]

DataGridView with Detail Edit Form - VS 2005[^]

Step 4: Inserting, Updating, and Deleting Data[^]

The above links are informative and provide examples.
 
Share this answer
 
instead of populating the Datagridview with a List object I am now using the following as my Datagridview.datasource
C#
public ObjectResult<presenters> GetPresentersList()
{
   // Check we have an ObjectContext
   if (entities == null) entities = new CompanySecretaryEntities();
      // Create a query from the entityset
      var query = from p in entities.Presenters
	          select p;
		
      ObjectQuery<presenters> presenters = (ObjectQuery<presenters>)query;

      return presenters.Execute(MergeOption.AppendOnly);
}</presenters></presenters></presenters>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900