【发布时间】:2020-04-29 14:13:05
【问题描述】:
我的 Winforms 程序中有一个 DataGridView 设置,其中包含从数据库填充的信息。
我使用Select * FROM [Data] 使用以下代码填充了DataGridView:
private void GetData(string selectCommand)
{
try
{
// Create a new data adapter based on the specified query.
dataAdapter = new OleDbDataAdapter(selectCommand, strConn);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand.
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable
{
Locale = CultureInfo.InvariantCulture
};
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
// Resize the DataGridView columns to fit the newly loaded content.
LogDataGridView.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (OleDbException) { }
}
【问题讨论】:
-
我们对您的数据源了解不够。可以离开加入状态信息吗?
-
@LarsTech 我编辑了帖子并添加了更多代码。我认为 left join 会起作用,我该怎么做?
-
如果可能的话,您可以直接在同一个查询中交叉连接两个数据库并生成一个已经包含状态的数据表。意思是,如果存储状态信息的数据库没有被其他进程独立更新。否则,您必须对其进行轮询。如果一定要轮询,只需要更新DataTable的一个Field,作为DGV的DataSoure。它将立即更新 UI。
-
@Jimi 我认为这正是我在查询中需要的,我的第二个访问数据库有 SO nr 和另一个包含状态的列。你能给我一个例子,说明我如何能够根据两个表之间共享的“SO nr”交叉连接这两个表吗?
标签: c# winforms datagridview oledb