By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

datagridview refresh

P: 2
using c# and vs2005
I am trying to refresh a datagridview control after updating a database, but despite everything

I've tried, no success.
When the app starts up, GridRefresh() is called to initially populate the dgv. This works fine.

The app then updates the database and calls GridRefresh() again expecting to see an updated row of the dgv with the updates, but no update -- just the original data.
The database is definitely being updated because if I restart the app, the dgv now has the updates.
My code is here
Expand|Select|Wrap|Line Numbers
  1. //code
  2.  public void GridRefresh()
  3.         {
  4.  
  5.             try
  6.             {
  7.                 OleDbConnection con = new OleDbConnection(strConnectionString);
  8.                 string strSql = "SELECT * FROM tblWMLog";
  9.                 OleDbDataAdapter ABMSadapter = new OleDbDataAdapter(strSql, strConnectionString);
  10.                 con.Open();
  11.                 DataSet ABMSdataset = new DataSet();
  12.                 OleDbCommandBuilder cmdbuilder = new OleDbCommandBuilder(ABMSadapter);
  13.                 ABMSadapter.Fill(ABMSdataset,"tblWMLog");
  14.                 bsource.DataSource = ABMSdataset.Tables["tblWMLog"];
  15.                 dgvDetector.DataSource = bsource;
  16.  con.Close();
  17.             }
  18.             catch (OleDbException ex)
  19.             {
  20.                 MessageBox.Show(ex.Message);
  21.             }
  22.         }
  23. //code for databse insert
  24.  
  25.  public void UpdateLog(string WMAgent,DateTime WMDateTime,double WMDuration)
  26.         {
  27.             try
  28.             {
  29.                 OleDbConnection con = new OleDbConnection(strConnectionString);
  30.                 string insert = "INSERT INTO tblWMlog(WMID,WMDateTime,WMDuration,Frequency,SName)"
  31.                      + "VALUES('" + WMAgent + "','" + WMDateTime + "','" + WMDuration + "','" + Convert.ToDouble(config.txtSFreq.Text) + "','" + config.txtSName.Text + "')";
  32.                 OleDbCommand command = new OleDbCommand(insert, con);
  33.                 con.Open();
  34.                 command.ExecuteNonQuery();
  35.                 con.Close();
  36.                 GridRefresh();
  37.  
  38.                 //ABMSadapter.Fill(ABMSdataset);
  39.                 //this.dgvDetector.DataSource = ABMSdataset.Tables[0];
  40.                 //con.Close();
  41.                 //this.dgvDetector.Rows.Add(1, WMAgent, WMDateTime, WMDuration, Convert.ToDouble(config.txtSFreq.Text), config.txtSName.Text);
  42.                 //this.dgvDetector.Refresh();
  43.  
  44.             }
  45.             catch (OleDbException ex)
  46.             {
  47.                 MessageBox.Show(ex.Message);
  48.             }
  49.  
  50.  
  51.         }
Dec 18 '08 #1
Share this Question
Share on Google+
3 Replies


nukefusion
Expert 100+
P: 221
Try calling ResetBindings(false) on your BindingSource.

You can also wrap your code samples in code tags, like this:

[code] code goes here [ /code]

so it looks like this

Expand|Select|Wrap|Line Numbers
  1.     // Here's my code sample
  2.  
Just makes it easier to read. :)
Dec 18 '08 #2

P: 2
i dint understand ur answer
Dec 19 '08 #3

nukefusion
Expert 100+
P: 221
@aleesha
Add this line of code after you rebind the grid.

Expand|Select|Wrap|Line Numbers
  1. bSource.ResetBindings(false)
  2.  
This will force the control to re-read all the items in the list. You can read more about this method here
Dec 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.