我有3个不同的datagridviews都绑定到一个单独的bindingsource。我想在将新数据加载到bindingsources时更新datagridviews。我在这里尝试了普遍接受的方法来解决这个问题:
1。)创建绑定源
2.。)将此对象的数据源设置为数据集表。
3.。)将DatagridView的数据源设置为绑定源对象。
bindingsrc.DataSource = newDataTable;
// The two lines below were supposedly a dirty solution to refreshing the grid.
dg1.DataSource = null;
dg1.DataSource = bindingsrc;
然而它并不起作用。我还尝试重置每个bindingsource的绑定:
bindingsrc.ResetBindings();
但无济于事。我知道我正在获取正确的新数据,因为一旦我在调试时进入代码,newDataTable就会有正确的新数据。所以这是datagridview没有刷新的问题。如果这可能是相关的,我的datagridviews是其父级是splitcontainer的面板的一部分。我也尝试刷新父母:
this.dataGridView.Parent.Refresh()
没有任何结果。
答案 0 :(得分:1)
我找到了解决问题的方法。我不需要处理BindingContext_Changed事件和一般方法:
bindingSource1.ResetBindings(false);
dataGridView1.DataSource = null;
bindingSource1.DataSource = null;
bindingSource1.DataSource = mytable;
dataGridView1.DataSource = bindingSource1;
是更新网格的正确方法。
真正的问题:从我原来的问题:"如果这可能是相关的,我的datagridviews是其父级是拆分容器的面板的一部分。"以下代码行:
split.Panel2.Controls.Add(new ConfigurableMatrices(comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString()));
重新添加了这个类(ConfigurableMatrices,其中包含我的datagridviews),其所有控件都添加到splitcontainer parent的第二个面板中。这行代码是从splitcontainer的另一半执行的。之后我也很爽快:
split.Panel2.Controls.Add(new ConfigurableMatrices(comboBox1.SelectedItem.ToString(),
comboBox2.SelectedItem.ToString()));
split.Panel2.Refresh();
但是这样,我每次只在顶部添加一个新类(ConfigurableMatrices),并且由于某种原因没有正确更新。现在适用于我的解决方案是在上面两行代码之前调用Dispose方法:
foreach (Control control in split.Panel2.Controls)
{
control.Dispose();
}
split.Panel2.Controls.Add(new ConfigurableMatrices(comboBox1.SelectedItem.ToString(), comboBox2.SelectedItem.ToString()));
split.Panel2.Refresh();
我希望Dispose方法是解决这个问题的正确方法,并且未来不会引发其他问题。
答案 1 :(得分:0)
在正常情况下,不需要刷新。以下打开Microsoft NorthWind数据库,将所有表名加载到带有DropDownStyle = DropDownList的ComboBox中。在ComboBox中选择一个表名,按下按钮,然后加载我们的DataGridView。我绕过使用DataSet,但除了DataGridView的DataMember属性之外没有区别,如果没有设置可能会导致手头的问题,所以最好检查Datamember是否正确。
获取表名的代码,并为我们提供连接字符串,无论是.accdb还是.mdb
在此代码中我们使用SELECT *但当然在真实的应用程序中我们单独选择特定操作所需的字段,除非需要查看所有字段。
Imports System.Data.OleDb
Public Class SchemaInfo
Private Shared _Instance As SchemaInfo
Public Shared Function GetInstance() As SchemaInfo
If _Instance Is Nothing Then
_Instance = New SchemaInfo
End If
Return _Instance
End Function
Protected Sub New()
End Sub
Private Shared _ConnectionString As String
Public Function ConnectionString() As String
Return _ConnectionString
End Function
Public Function TableNames(ByVal DatabaseName As String) As List(Of String)
Dim Names As New List(Of String)
Using cn As New OleDbConnection(BuildConnectionString(DatabaseName))
_ConnectionString = cn.ConnectionString
cn.Open()
Dim dt As DataTable = cn.GetSchema("Tables", New String() {Nothing, Nothing, Nothing, "Table"})
For Each row As DataRow In dt.Rows
Names.Add(row.Field(Of String)("Table_Name"))
Next
End Using
Return Names
End Function
<System.Diagnostics.DebuggerStepThrough()> _
Private Shared Function BuildConnectionString(ByVal DatabaseName As String) As String
If IO.Path.GetExtension(DatabaseName).ToLower = ".accdb" Then
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = DatabaseName
}
Return Builder.ConnectionString
ElseIf IO.Path.GetExtension(DatabaseName).ToLower = ".mdb" Then
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.Jet.OLEDB.4.0",
.DataSource = DatabaseName
}
Return Builder.ConnectionString
Else
Throw New Exception("File type not supported")
End If
End Function
End Class
表格代码
Imports System.Data.OleDb
Public Class SwitchTableOnBindingSourceForm
WithEvents bsData As New BindingSource
Private Sub SwitchTableOnBindingSourceForm_Load(
sender As Object, e As EventArgs) Handles MyBase.Load
cboTables.DataSource = SchemaInfo.GetInstance.TableNames(
IO.Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "NorthWind.accdb"))
DataGridView1.DataSource = bsData
End Sub
Private Sub cmdSelectTable_Click(
sender As Object, e As EventArgs) Handles cmdSelectTable.Click
Dim dt As New DataTable
Using cn As New OleDbConnection(SchemaInfo.GetInstance.ConnectionString)
Using cmd As New OleDbCommand("SELECT * FROM " & cboTables.Text, cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
End Using
bsData.DataSource = dt
End Sub
End Class