8 Replies - 30298 Views - Last Post: 23 October 2008 - 02:15 PM Rate Topic: -----

#1 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

refresh DataGridView

Post icon  Posted 23 October 2008 - 09:32 AM

I have a DataGridView on the MainForm, and I've put the database query and the resulting information and populating in a function... When someone "Clicks" one DataGridView, it populates this other DataGridView with the relevant information from the database... That works just fine... But when I call that function from a form that is inserting another record and then closing, it does not repopulate the DataGridView...

I step through it all and it seems to be doing everything it's supposed to do, but it just acts like the new record isn't in there... Almost as if the DataGridView is cached or something... I've tried several methods of clearing and refreshing, but it does not seem to make any difference... If I go an re-"Click" on the other DataGridView, it will repopulate the DataGridView and show the new entry...

Here is my insert code on the TicketsQueries.cs class form
		public static BindingSource GetTicketsByCustomerID(int CustomerID)
		{
			string sSQL = "GetTicketsByCustomerID";
			SqlConnection cnGetRecords = new SqlConnection(DataAccess.GetConnectionString("MPCS.Properties.Settings.MassageProConnectionString"));
			SqlCommand cmdGetRecords = new SqlCommand();
			SqlDataAdapter daGetRecords = new SqlDataAdapter();
			DataSet dsGetRecords = new DataSet();
			cmdGetRecords.Parameters.Clear();
			cmdGetRecords.CommandText = sSQL;
			cmdGetRecords.CommandType = CommandType.StoredProcedure;
			cmdGetRecords.Parameters.AddWithValue("@CustomerID", CustomerID);
			cmdGetRecords.Connection = cnGetRecords;
			DataAccess.HandleConnection(cnGetRecords);
			BindingSource oBindingSource = DataAccess.GetBindingSource(cmdGetRecords);
			return oBindingSource;
		}



Here is my code on the MainForm.cs form, where the user clicks on the DataGridView and triggers the next DataGridView to populate...
		public void loadTicketsDataGrid(int CID)
		{
			ticketsDataGridView.DataSource = TicketsQueries.GetTicketsByCustomerID(CID);

			ticketsDataGridView.Columns[0].Name = "TicketID";
			ticketsDataGridView.Columns[0].DataPropertyName = "TicketID";
			ticketsDataGridView.Columns[0].Width = 5;
			ticketsDataGridView.Columns[0].HeaderText = "";
			ticketsDataGridView.Columns[0].Visible = false;

			ticketsDataGridView.Columns[1].Name = "TicketDate";
			ticketsDataGridView.Columns[1].DataPropertyName = "TicketDate";
			ticketsDataGridView.Columns[1].Width = 110;
			ticketsDataGridView.Columns[1].HeaderText = "Date";

			ticketsDataGridView.Columns[2].Name = "TicketDescription";
			ticketsDataGridView.Columns[2].DataPropertyName = "TicketDescription";
			ticketsDataGridView.Columns[2].Width = 200;
			ticketsDataGridView.Columns[2].HeaderText = "Description";

			ticketsDataGridView.Columns[3].Name = "TherapistID";
			ticketsDataGridView.Columns[3].DataPropertyName = "TherapistID";
			ticketsDataGridView.Columns[3].Width = 5;
			ticketsDataGridView.Columns[3].HeaderText = "";
			ticketsDataGridView.Columns[3].Visible = false;

			ticketsDataGridView.Columns[4].Name = "CustomerID";
			ticketsDataGridView.Columns[4].DataPropertyName = "CustomerID";
			ticketsDataGridView.Columns[4].Width = 5;
			ticketsDataGridView.Columns[4].HeaderText = "";
			ticketsDataGridView.Columns[4].Visible = false;

			ticketsDataGridView.Columns[5].Name = "TicketBillableTime";
			ticketsDataGridView.Columns[5].DataPropertyName = "TicketBillableTime";
			ticketsDataGridView.Columns[5].Width = 75;
			ticketsDataGridView.Columns[5].HeaderText = "Time";

			ticketsDataGridView.Columns[6].Name = "TicketBillableRate";
			ticketsDataGridView.Columns[6].DataPropertyName = "TicketBillableRate";
			ticketsDataGridView.Columns[6].Width = 75;
			ticketsDataGridView.Columns[6].HeaderText = "Rate";
			this.ticketsDataGridView.Columns[6].DefaultCellStyle.Format = "c";

			ticketsDataGridView.Columns[7].Name = "TicketTotal";
			ticketsDataGridView.Columns[7].DataPropertyName = "TicketTotal";
			ticketsDataGridView.Columns[7].Width = 75;
			ticketsDataGridView.Columns[7].HeaderText = "Total";
			this.ticketsDataGridView.Columns[7].DefaultCellStyle.Format = "c";

			ticketsDataGridView.Columns[8].Name = "TicketPaid";
			ticketsDataGridView.Columns[8].DataPropertyName = "TicketPaid";
			ticketsDataGridView.Columns[8].Width = 75;
			ticketsDataGridView.Columns[8].HeaderText = "Paid";

			ticketsDataGridView.Columns[9].Name = "TicketProfit";
			ticketsDataGridView.Columns[9].DataPropertyName = "TicketProfit";
			ticketsDataGridView.Columns[9].Width = 5;
			ticketsDataGridView.Columns[9].HeaderText = "";
			ticketsDataGridView.Columns[9].Visible = false;
		}

		private void customersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
			string tempVar = customersDataGridView.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
			CID = int.Parse(tempVar);
			if (therapistName != "admin")
			{
				addTicketToolStripButton.Visible = true;
				addTicketToolStripMenuItem.Enabled = true;
				loadTicketsDataGrid(CID);
			}
			editCustomerToolStripMenuItem.Enabled = true;
		}



Here is the code of the AddTicketForm.cs form where a user adds a new entry and it calls the function to populate the DataGridView again with the new entry...
				TicketsQueries.InsertTicket(ticketDateTimePicker.Value, ticketDescriptionTextBox.Text, TID, CID, billedTime, billedRate, ticketTotal, ticketPaidComboBox.Text, ticketProfit);
				MainForm mForm = new MainForm(TID, therapistName);
				mForm.loadTicketsDataGrid(CID);
				this.Close();



Is This A Good Question/Topic? 0
  • +

Replies To: refresh DataGridView

#2 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:00 AM

Im not 100% sure on this but after you re-populate your datagrid

you should also call:

datagridview.Refresh();
datagridview.Parent.Refresh();

In that order.
I dont have an IDE in front of me but it should be very similar to that.
Was This Post Helpful? 0
  • +
  • -

#3 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:08 AM

Thank you marcells23, but I've already tried that, but just for grins, I tried it again, just like you said, and it didn't do anything...
Was This Post Helpful? 0
  • +
  • -

#4 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:13 AM

Hmm..have you tried refreshing your binding source or re-filling your dataset along with the refresh?
Was This Post Helpful? 0
  • +
  • -

#5 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:27 AM

Yes, it does that when the function is called, it re-queries, creates the bindingsource and everything...

Note: I just created a delete entry function on the MainForm and it calls the DataGridView refresh function after the deletion, and it works correctly... So it would seem that it is only not working when the function is called from a different form, although, as I mentioned, when I stepped through it, it worked the function just like as if it was called from the MainForm...
Was This Post Helpful? 0
  • +
  • -

#6 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:39 AM

Ahh that makes a little more sense..you cant refresh that control from another form.

MSDN has an example of how to do this.
Multithreaded Windows Form Control


Quote

The STA model requires that any methods on a control that need to be called from outside the control's creation thread must be marshaled to (executed on) the control's creation thread. The base class Control provides several methods (Invoke, BeginInvoke, and EndInvoke) for this purpose. Invoke makes synchronous method calls; BeginInvoke makes asynchronous method calls.

Was This Post Helpful? 0
  • +
  • -

#7 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:46 AM

That's way beyond me... I would think then I need to go back to concentrating on using that function from the MainForm upon closing of the other form...

I had tried to use a AddTicketForm.FormClosing event from the MainForm, but that didn't work... Perhaps some kind of MainForm.Focus event... ???
Was This Post Helpful? 0
  • +
  • -

#8 marcells23  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 143
  • Joined: 22-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 10:59 AM

Its a little above my head also but im not sure there is another way to do it.
Was This Post Helpful? 0
  • +
  • -

#9 webwired  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 33
  • View blog
  • Posts: 339
  • Joined: 26-August 07

Re: refresh DataGridView

Posted 23 October 2008 - 02:15 PM

What about "MdiChildActivate Form Closing Event" ?

I would have thought something like this would have worked...

		private void MainForm_Load(object sender, EventArgs e)
		{
			AddTicketForm addTicketForm = new AddTicketForm(CID, TID, therapistName);
			addTicketForm.MdiChildActivate += new System.EventHandler(MainForm_MdiChildActivate);
		}

		private void MainForm_MdiChildActivate(object sender, EventArgs e)
		{
			loadTicketsDataGrid(CID);
		}



But, it did not... Anyone have any suggestions?

This post has been edited by webwired: 23 October 2008 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1