我正在vb.net 2010中设计一个Windows窗体。它是一个带有两个控件的简单窗体。一个是datagridview以表格形式显示数据,另一个是图表控件,它从gridview获取数据以绘制图表。我还有一个组合框,它有助于选择我想要的表或图表的变量。此外,还有两个按钮,一个标记为“表格”,点击其中,使用外部Excel文件中的数据填充网格视图。标有“plot”的另一个按钮绘制了导入gridview的数据。
假设我在comboBox中有三个变量,'实际GDP',消费,投资。在运行期间,我第一次选择任何变量时,它工作正常。但在同一次运行期间,如果我选择另一个变量,则gridview可以导入变量但绘图无法绘制图表。 Plot无法找到变量,因为它仍在寻找第一个变量,而在gridview中我有第二个变量的表。如果我们在显示第一个变量后选择另一个变量,那么gridview和plot之间似乎存在脱节。
如果我关闭运行并重建并选择另一个变量,它可以正常工作。但是在那次运行期间,如果我选择另一个变量,它就会失败。这是我的代码:
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 " & 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>=", myValStr, " ", "and", " ", "FY<=", 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
答案 0 :(得分:0)
您应该尝试将数据源绑定到BindingSource并将绑定源绑定到数据网格
BindingSourceExample.DataSource = DataToBind
DataGridView.DataSource = BindingSourceExample
至少我会这样做!