Link to home
Start Free TrialLog in
Avatar of John Porter
John PorterFlag for United States of America

asked on

Can't clear a DataGridView before Insert method is called to refresh the screen...

Hi,
Does anyone know how to clear a DaGridView control?

I am binging a DataGridView control in the Load method of a windows form. I then change some data and call an Update method. The data updates but the DataGridView dosn't reflect the changes unless I open and close the form.

Is there a way to refresh the screen without having to open and reclose??

 Refresh() doesn't seem to work.

My Code:

When Form Loads:
Conn = new OleDbConnection(ConStr))
 cmd = new OleDbCommand(SQL, Conn))
 Conn.Open();
 da = new OleDbDataAdapter())
 da.SelectCommand = cmd;
 oleDbCb = new OleDbCommandBuilder(da))
da.Fill(dataSet1, "Stats");
dataGridView1.DataSource = dataSet1;
dataGridView1.DataMember = "Stats";

Insert Method:
string connectionString = LoginFirst.Connection;
                OleDbConnection connection = new OleDbConnection(connectionString);
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = "spInsertTrawlT";
                command.CommandType = CommandType.StoredProcedure;
                OleDbDataReader dr = command.ExecuteReader();
                da.InsertCommand = command;
                connection.Close();

If I call the load method after the Insert method, the DataGridView fills agin but rows are doubled. Is there a method to clear the DataGridView before reloading it??
ASKER CERTIFIED SOLUTION
Avatar of Justin_W
Justin_W

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of John Porter

ASKER

Hi Justin - Thanks for the response!

I tried your suggestion but have same results.

Is it the underlying DataSet instance(dataSet1) that is not clearing?? So I tried this:

InsertNewRecordMethod();
                this.dataGridView1.EndEdit();
               this.dataGridView1.Refresh();
               OleDbConnection Conn = new OleDbConnection(ConStr);
               cmd = new OleDbCommand(SQL, Conn);
               Conn.Open();
               da = new OleDbDataAdapter();
               da.SelectCommand = cmd;
               OleDbCommandBuilder oleDbCb = new OleDbCommandBuilder(da);
 
//fill dataadapter with a phony dataset to clear the da.fill              
da.Fill(dataSet1, "Empty");
               dataGridView1.DataSource = dataSet1;
               dataGridView1.DataMember = "Empty";
           
             //reload dataadapter with real datatable ("Stats") in the LoadMethod
             LoadForm();

This produces no exceptions but has same result in datagridView control: Shows new record but doubles all previous records!!

Any thoughts?

Thanks!!
Avatar of Justin_W
Justin_W

In your most recent post, the Fill method is adding all existing records, but not clearing out the old copies. First clear/remove all existing records from your dataset/datatable, then use Fill.

Also, why are you Filling into a new DataTable? You should probably be filling into the same DataTable and not changing the DataMember.
Got it...

Its working great now by clearing the dataSet before filling as you suggested Justin.

Thank you very much!!
You're welcome!