datagridview refresh

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aleesha
    New Member
    • Dec 2008
    • 2

    datagridview refresh

    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
    [code=cpp]
    //code
    public void GridRefresh()
    {

    try
    {
    OleDbConnection con = new OleDbConnection (strConnectionS tring);
    string strSql = "SELECT * FROM tblWMLog";
    OleDbDataAdapte r ABMSadapter = new OleDbDataAdapte r(strSql, strConnectionSt ring);
    con.Open();
    DataSet ABMSdataset = new DataSet();
    OleDbCommandBui lder cmdbuilder = new OleDbCommandBui lder(ABMSadapte r);
    ABMSadapter.Fil l(ABMSdataset," tblWMLog");
    bsource.DataSou rce = ABMSdataset.Tab les["tblWMLog"];
    dgvDetector.Dat aSource = bsource;
    con.Close();
    }
    catch (OleDbException ex)
    {
    MessageBox.Show (ex.Message);
    }
    }
    //code for databse insert

    public void UpdateLog(strin g WMAgent,DateTim e WMDateTime,doub le WMDuration)
    {
    try
    {
    OleDbConnection con = new OleDbConnection (strConnectionS tring);
    string insert = "INSERT INTO tblWMlog(WMID,W MDateTime,WMDur ation,Frequency ,SName)"
    + "VALUES('" + WMAgent + "','" + WMDateTime + "','" + WMDuration + "','" + Convert.ToDoubl e(config.txtSFr eq.Text) + "','" + config.txtSName .Text + "')";
    OleDbCommand command = new OleDbCommand(in sert, con);
    con.Open();
    command.Execute NonQuery();
    con.Close();
    GridRefresh();

    //ABMSadapter.Fil l(ABMSdataset);
    //this.dgvDetecto r.DataSource = ABMSdataset.Tab les[0];
    //con.Close();
    //this.dgvDetecto r.Rows.Add(1, WMAgent, WMDateTime, WMDuration, Convert.ToDoubl e(config.txtSFr eq.Text), config.txtSName .Text);
    //this.dgvDetecto r.Refresh();

    }
    catch (OleDbException ex)
    {
    MessageBox.Show (ex.Message);
    }


    }[/code]
    Last edited by Frinavale; Dec 18 '08, 05:34 PM. Reason: added [code] tags
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    Try calling ResetBindings(f alse) 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

    Code:
        // Here's my code sample
    Just makes it easier to read. :)

    Comment

    • aleesha
      New Member
      • Dec 2008
      • 2

      #3
      i dint understand ur answer

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Originally posted by aleesha
        i dint understand ur answer
        Add this line of code after you rebind the grid.

        Code:
        bSource.ResetBindings(false)
        This will force the control to re-read all the items in the list. You can read more about this method here

        Comment

        Working...