John Porter
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(connection String);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "spInsertTrawlT";
command.CommandType = CommandType.StoredProcedur e;
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??
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(connection
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "spInsertTrawlT";
command.CommandType = CommandType.StoredProcedur
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Also, why are you Filling into a new DataTable? You should probably be filling into the same DataTable and not changing the DataMember.
ASKER
Got it...
Its working great now by clearing the dataSet before filling as you suggested Justin.
Thank you very much!!
Its working great now by clearing the dataSet before filling as you suggested Justin.
Thank you very much!!
You're welcome!
ASKER
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!!