title: How to update data in the datagridview during run time in VB.NET 2010
body: <p>I am designing a windows form in vb.net 2010. It's a simple one with two controls. One is datagridview to display data in tabular form and another is a chart control which fetches data from the gridview to draw charts. I also have a combobox which helps to select the variable for which I would like to have the table or charts. In addition, there are two buttons, one labeled 'table' clicking on which populates the gridview with data from an external excel file. Another button labeled 'plot' plots the data that was imported into gridview. </p>
  
  <p>Suppose I have three variables in the comboBox, 'Real GDP', consumption, investment. During the run-time, the first time I select any variable, it works fine.But during the same run, if I select another variable, then gridview can import the variable but plot fails to draw the chart. <b> Plot can not find the variable because it is still looking for the first variable while in gridview I have the table for the second variable. It seems there is a disconnect between the gridview and plot if we select another variable after the first one is displayed. </b></p>
  
  <p>If I close the run and rebuild and select another variable it works fine. But again during that run if I select another variable, it fails. Here is my code:</p>
  
  <pre><code>Public Class Form1
  Dim MyConnection As System.Data.OleDb.OleDbConnection
  Dim DtSet As System.Data.DataSet
  Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
  
  Dim myStr1, myStr2, myStr3 As String
  Dim msgStr, msgEnd, defValStr, defValEnd, _
     title, myValStr, myValEnd As String
  
  Public Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      myStr1 = selectVariable.SelectedItem
      MsgBox("You have chosen " &amp; myStr1)
      ' Defining a InputBox to enter Sample Period
      ' Start Period
      msgStr = "Enter your Starting time period: Between 1980 and 2012"
      defValStr = "1982"
      title = "Define Sample Time Period"
      myValStr = InputBox(msgStr, title, defValStr)
      ' End Period
      msgEnd = "Enter your Ending time period: Between 1980 and 2012"
      defValEnd = "2012"
      title = "Define Sample Time Period"
      myValEnd = InputBox(msgEnd, title, defValEnd)
      Select Case myStr1
          Case "Real GDP"
              myStr2 = "xgdp"
          Case "Consumption"
              myStr2 = "CP"
          Case "Investment"
              myStr2 = "IP"
      End Select
  
      myStr3 = String.Concat("select", " ", "FY", ",", myStr2, " ", "from", " ", "[Dataset$]", " ", "where", " ", "FY&gt;=", myValStr, " ", "and", " ", "FY&lt;=", myValEnd)
      MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='J:\Rushad\MOF\EViews Stuff\Data_Macro_Model.xls';Extended Properties=Excel 8.0;")
      MyCommand = New System.Data.OleDb.OleDbDataAdapter(myStr3, MyConnection)
      MyCommand.TableMappings.Add("Table", "Net-informations.com")
      DtSet = New System.Data.DataSet
      MyCommand.Fill(DtSet)
      DataGridView1.DataSource = DtSet.Tables(0)
      MyConnection.Close()
  End Sub
  
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
      Chart1.DataSource = DtSet
      Chart1.Series.Add(myStr2)
      Chart1.Series(myStr2).XValueMember = "FY"
      Chart1.Series(myStr2).YValueMembers = myStr2
  End Sub
  
  End Class
  </code></pre>