Refresh DataGridView

Jelmer

Well-known member
Joined
Jan 11, 2006
Messages
96
Hello,

Ive got a form with a datagridview, if i press the mouse button on a record some textfields are filled with the data of that row.

You can change the data in the textfields, and press a button to save the changes. The result is that the changes are saved into the MDB database.

But my datagridview doesnt wanne show the new data. Only if i restart the whole program. Ill tried dataviewgrid1.refresh(), but that doesnt help me.

Both do use another connection to the database.
The datagridview is build with the wizzard.
The save function is in a class.

How can i refresh or close and open the databaseconnection ?
The datasource of the dataviewgrid is: productenCategorieBindingSource1

Thnx

Jelmer
 
If your data source is a dataset then do something like this: dataset.clear
and after that fill the dataset again
If you do not use a dataset, then simply reconnect to the mdb file again
 
If i clear the dataset the dataviewgrid is empty.. so thats okay.
(billingdataset11)

But now i need to fill it again..

The Datasource of the dataviewgrid is: productenCategorieBindingSource1
The Productencategoriebindingsource1.Datasource = billingDataset11

But how can i fill it again ? :rolleyes: :o

If i close the form and open it again (i press a button from the main form) than is the data there but not the updated data.. i need to restart the application for the new data... thats not ok..
 
Last edited by a moderator:
If the controls and grid are bound to the same DataSet then the changes should update immediately. If you use a different dataset, youll have to manually refresh the grids dataset after you save to the DB.

The two connections shouldnt matter - it comes down to what youre binding to the grid.

-ner
 
Nerseus said:
If the controls and grid are bound to the same DataSet then the changes should update immediately. If you use a different dataset, youll have to manually refresh the grids dataset after you save to the DB.

The two connections shouldnt matter - it comes down to what youre binding to the grid.

-ner

Ill add or change some data in a class..
The grid on the form is another datagrid.

But if i refresh that datagrid, ill still get old data..

This is the code how i add data to the mdb database:

Code:
                OleDbConnection Connection = new OleDbConnection();
                Connection = dbase(Connection);
                // Create an OleDb command,  
                OleDbCommand command = new OleDbCommand();
                command.Connection = Connection;
                command.CommandText = "insert into Producten (Naam,Prijs,Opmerkingen,Categorie,BTW) values(" + Naam + "," + Prijs + "," + Opmerkingen + "," + Categorie + "," + BTW + ") ";
                //MessageBox.Show("update Bedrijven set Bedrijf = " + Bedrijf  + ", Contactpersoon = "  + Contactpersoon + ", Adres = " + Adres + ", Postcode = " + Postcode + ", Plaats = " + Plaats + ", Tel = " +  Tel + ", Mobiel = " + Mobiel + ", Fax = " + Fax + ", Email = " + Email + ", Website = "+ Website + ", kvk = " + KVK + ", BTW = " + BTW + ", debiteurnr = " + Debiteurnr + ", opmerkingen = " + Opmerkingen + " where id = " + ID);
                command.ExecuteNonQuery();

After this code is runned, the data is in de database !
But the grid doesnt show that data..

After a restart of the whole program i can see the changed data in the grid.
So i need to deconnect the Bindingsources ore something ?
But i cant find it.. i build them in a wizzard..

Used to work with Delphi.. there its a pice of cake... connection.false; and connection.treu; and youll get the recent data.
 
If you are binding to a DataSet / DataTable then you will need to refresh its contents after you have updated the DB as the Dataset does not maintain a connection to the DB it cannot automatically update itself.

Just use the same dataadapter.Fill you used to populate the datatable originally.
 
That code above is used in a class.
So i cant use the dataset of the form where i am.
First i press a butten, that uses the class, and adds something to the database.

After that i will show it on the form.
Ive got a DataGridView1 that uses as datasource productenCategorieBindingSource1 that uses as datasource billingdataset11.

With billingdataset11.clear(); i can clear the list.
But how to fill the data ... wich commands.. ????

Ive tried to connect.. doesnt work, refresh isnt available, reload not.. etc.

Ill use .Net.

why is that so difficult.. ist as easy as that.. :( , its easier to build a datagridview by myzelf.. :rolleyes:
 
I think your missing the point of what people are suggesting (or maybe I am). If your application works when you open it new, then whatever code you are using to populate the DataGridView in the firstplace can be used to refresh the info. Simply clear all the information then use the same lines of code as your app uses on load to re-populate it.
 
Just use DataGridView1.DataBind(). Or, if needed, rebind everything after you perform the update:

Code:
With DataGridView1
.DataSource = productenCategorieBindingSource1
.DataMember = "<whatever member table you are bound to, if any>"
.DataBind()
End With

Todd
 
Last edited by a moderator:
PlausiblyDamp said:
C# is case sensitive - are you typing it correctly?

Yups.. the error is:

System.Windows.Forms.DataGridView does not contain a definition for DataBind

Or do i need to include more things?:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using products;
using Ini;
using statistieken;
 
All of the code works.. exept the last rule:

Code:
                    dataGridView1.DataSource = productenCategorieBindingSource1;
                    dataGridView1.DataMember = "";
                    dataGridView1.DataBind();

And of course dataGridView1 is of the correct syntax.
In the design mode its an memboer of: System.Windows.Forms.DataGridView
 
Back
Top