7 Replies - 3929 Views - Last Post: 17 January 2010 - 09:02 AM Rate Topic: -----

#1 eURe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 27-October 09

Update Datagridview

Posted 16 January 2010 - 09:38 AM

Hi There;

I have a datagridview that filled with Norhwind's customer table, when double click on the selected record on the gridview, a window will open and display the data for the selected records. Once the 'Save' button is clicked on the new window after the changes is done, how could i update the datagridview to reflect the changes done?

I have attached here with my sample application that connect to Northwind database (SQL Server 2005) and work with Visual Studio 2008,

please help!!!!

Thank you very much...

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Update Datagridview

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Update Datagridview

Posted 16 January 2010 - 10:27 AM

Hi there and welcome to DIC you DICHead. :P

First off, please post the code only. More importantly make sure that you post that is relevant to your problem.

You say you don't know how to insert values into a Database, right? Post the class where you are doing all the Database mumbo-jumbo. That way we can provide an example with your variable names and you'll be able to see what's happening much easier.

Can't wait! See ya soon! :D
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Update Datagridview

Posted 16 January 2010 - 09:15 PM

View Postpapuccino1, on 16 Jan, 2010 - 01:27 PM, said:

Hi there and welcome to DIC you DICHead. :P

First off, please post the code only. More importantly make sure that you post that is relevant to your problem.

You say you don't know how to insert values into a Database, right? Post the class where you are doing all the Database mumbo-jumbo. That way we can provide an example with your variable names and you'll be able to see what's happening much easier.

Can't wait! See ya soon! :D


His post said nothing about not being able to insert data. His problem is AFTER he does an insert from a child form, he wants to update the datagridview to reflect the newly inserted data.

eURe, hopefully you are using ShowDialog to show the child form. If so, after the ShowDialog, simple call the method that you use to show the data in the grid. This will now contain the new or updated data since its reading from the database.

I am posting from my phone so I am unable to see your code.
Was This Post Helpful? 0
  • +
  • -

#4 eURe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 27-October 09

Re: Update Datagridview

Posted 16 January 2010 - 10:03 PM

Thanks for you guy looking at this... here ard my code..

Below is the code in Form1,
//When any row of record in the datagridview is click, it pass the data into Form2 
private void customersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			System.Data.DataRowView SelectedRowView;
			NorthwindDataSet.CustomersRow SelectedRow;

			SelectedRowView = (System.Data.DataRowView)customersBindingSource.Current;
			SelectedRow = (NorthwindDataSet.CustomersRow)SelectedRowView.Row;

			Form2 CustomerEntry = new Form2();
			CustomerEntry.LoadCustomer(SelectedRow.CustomerID);
			CustomerEntry.MdiParent = this.MdiParent;
			CustomerEntry.Show();
		}




Below the code in Form2

//Loading data based on the record click in Form1 from datagridview
internal void LoadCustomer(string cust_id)
		{
			customersTableAdapter.FillByCustomerID(northwindDataSet.Customers, cust_id);
			
		}

//Saving the changes in the database
		private void Save_Click(object sender, EventArgs e)
		{
			this.Validate();
			this.customersBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.northwindDataSet);
			this.Close();

		}



Once the Save button is click, it sould save the record and i would like to have the datagridview in the Form1 have shown the latest record.

How to go for this?

Let me know if it's not clear....
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Update Datagridview

Posted 17 January 2010 - 06:22 AM

on Form2, when clicking the Save button...

		private void Save_Click(object sender, EventArgs e)
		{
			this.Validate();
			this.customersBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.northwindDataSet);

			this.DialogResult = DialogResult.OK;

			this.Close();

		}



now on the parent form where the datagridview is, you will want to do this...

private void customersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			System.Data.DataRowView SelectedRowView;
			NorthwindDataSet.CustomersRow SelectedRow;

			SelectedRowView = (System.Data.DataRowView)customersBindingSource.Current;
			SelectedRow = (NorthwindDataSet.CustomersRow)SelectedRowView.Row;

			Form2 CustomerEntry = new Form2();
			CustomerEntry.LoadCustomer(SelectedRow.CustomerID);
			CustomerEntry.MdiParent = this.MdiParent;

			if (CustomerEntry.ShowDialog() == DialogResult.OK)
			{
				  // this will rebind the data to the datagridview
				  //   so that the newest changes will show up.
				  this.customersTableAdapter.Fill(this.northwindDataSet.Customers); 
			}
		}


Was This Post Helpful? 0
  • +
  • -

#6 eURe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 27-October 09

Re: Update Datagridview

Posted 17 January 2010 - 07:48 AM

View Posteclipsed4utoo, on 17 Jan, 2010 - 05:22 AM, said:

on Form2, when clicking the Save button...

		private void Save_Click(object sender, EventArgs e)
		{
			this.Validate();
			this.customersBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.northwindDataSet);

			this.DialogResult = DialogResult.OK;

			this.Close();

		}



now on the parent form where the datagridview is, you will want to do this...

private void customersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			System.Data.DataRowView SelectedRowView;
			NorthwindDataSet.CustomersRow SelectedRow;

			SelectedRowView = (System.Data.DataRowView)customersBindingSource.Current;
			SelectedRow = (NorthwindDataSet.CustomersRow)SelectedRowView.Row;

			Form2 CustomerEntry = new Form2();
			CustomerEntry.LoadCustomer(SelectedRow.CustomerID);
			CustomerEntry.MdiParent = this.MdiParent;

			if (CustomerEntry.ShowDialog() == DialogResult.OK)
			{
				  // this will rebind the data to the datagridview
				  //   so that the newest changes will show up.
				  this.customersTableAdapter.Fill(this.northwindDataSet.Customers); 
			}
		}



Hi eclipsed4utoo;

Thanks for your solution, it's work fine if it's not in MDI container.However, my application desinged in MDI where it doesn't allow to have 'ShowDialog()' method.

E.g. Following code is not allowed in MDI :(
			CustomerEntry.MdiParent = this.MdiParent;

			if (CustomerEntry.ShowDialog() == DialogResult.OK)
			{
				  // this will rebind the data to the datagridview
				  //   so that the newest changes will show up.
				  this.customersTableAdapter.Fill(this.northwindDataSet.Customers); 
			}
		}



How could i go for this? Help~~

This post has been edited by eURe: 17 January 2010 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Update Datagridview

Posted 17 January 2010 - 08:18 AM

wait...so the form that has the datagridview is an MDI form? why? normally, you have a "Main" form that is the MDI with no controls other than menus. Then the menus open child forms, and those forms are what have the controls such as a datagridview.

So yours is like

MDI Parent --> child form with save button

rather than...

MDI Parent --> child form with datagridview --> child form with save button

is that correct?

EDITED: ok, nevermind, I think I know what you mean now.

You could pass a reference of the Form1 to Form2. Then before you close Form2 after a save, call the code to update the datagridview.

// form level variable
private Form1 m_form1;

public Form2(Form1 form1)
{
	m_form1 = form1;
}

		private void Save_Click(object sender, EventArgs e)
		{
			this.Validate();
			this.customersBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.northwindDataSet);

			m_form1.customersTableAdapter.Fill(m_form1.northwindDataSet.Customers); 

			this.Close();

		}



I haven't tested it, so I am not totally positive it would work.

This post has been edited by eclipsed4utoo: 17 January 2010 - 08:27 AM

Was This Post Helpful? 1
  • +
  • -

#8 eURe  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 27-October 09

Re: Update Datagridview

Posted 17 January 2010 - 09:02 AM

View Posteclipsed4utoo, on 17 Jan, 2010 - 07:18 AM, said:

wait...so the form that has the datagridview is an MDI form? why? normally, you have a "Main" form that is the MDI with no controls other than menus. Then the menus open child forms, and those forms are what have the controls such as a datagridview.

So yours is like

MDI Parent --> child form with save button

rather than...

MDI Parent --> child form with datagridview --> child form with save button

is that correct?

EDITED: ok, nevermind, I think I know what you mean now.

You could pass a reference of the Form1 to Form2. Then before you close Form2 after a save, call the code to update the datagridview.

// form level variable
private Form1 m_form1;

public Form2(Form1 form1)
{
	m_form1 = form1;
}

		private void Save_Click(object sender, EventArgs e)
		{
			this.Validate();
			this.customersBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.northwindDataSet);

			m_form1.customersTableAdapter.Fill(m_form1.northwindDataSet.Customers); 

			this.Close();

		}



I haven't tested it, so I am not totally positive it would work.


Thanks again for your help, my approach is
MDI Parent --> run with child Form1 with datagridview --> child Form2 with save button

Your gut feeling should make the above code work... and it's work for me.. :)

Thanks again eclipsed4utoo :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1