C# 在FbRemoteEvent上填充DataGridView
我有一个MainForm(带有dataGridView)和DataModule类以及我自己的LoadDataTable方法,它们执行FbCommand select sql并在dataset和FbRemoteEvent catch方法中填充datatable。当我运行我的应用程序时,在MainForm_OnLoad中调用LoadDataTable方法,我的dataGridView成功显示数据。但当我的应用程序从服务器捕获FbRemoteEvent并调用LoadDataTable方法时,dataGridView中发生了异常。为什么? MainForm:C# 在FbRemoteEvent上填充DataGridView,c#,winforms,visual-studio,datagridview,firebird,C#,Winforms,Visual Studio,Datagridview,Firebird,我有一个MainForm(带有dataGridView)和DataModule类以及我自己的LoadDataTable方法,它们执行FbCommand select sql并在dataset和FbRemoteEvent catch方法中填充datatable。当我运行我的应用程序时,在MainForm_OnLoad中调用LoadDataTable方法,我的dataGridView成功显示数据。但当我的应用程序从服务器捕获FbRemoteEvent并调用LoadDataTable方法时,dataG
public partial class MainForm : Form
{
private readonly DataModule _dataModule = DataModule.GetInstance();
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = _dataModule.AppDataSet;
dataGridView1.DataMember = "MESSAGEQUEUE";
_dataModule.LoadMessageQueueDataTable();
}
}
private void FirebirdRemoteEventOnRemoteEventCounts(object sender, FbRemoteEventEventArgs fbRemoteEventEventArgs)
{
switch (fbRemoteEventEventArgs.Name.Trim().ToUpper())
{
case "QUEUE_NEW_MESSAGE":
if (fbRemoteEventEventArgs.Counts > 0)
{
LoadMessageQueueDataTable();
}
break;
}
}
public void LoadMessageQueueDataTable()
{
if (ConnectToFirebird())
{
using (var firebirdTransaction = FirebirdConnection.BeginTransaction())
{
using (var firebirdCommand = new FbCommand
{
Connection = firebirdTransaction.Connection,
Transaction = firebirdTransaction,
CommandType = CommandType.Text,
CommandText = "select MESSAGEQUEUEID, CREATEDATETIME, SENDER, RECIPIENT, TEXT from MESSAGEQUEUE"
})
{
AppDataSet.Tables["MESSAGEQUEUE"].Clear();
try
{
AppDataSet.Tables["MESSAGEQUEUE"].Load(firebirdCommand.ExecuteReader());
firebirdCommand.Transaction.Commit();
}
catch (FbException firebirdException)
{
firebirdCommand.Transaction.Rollback();
}
}
}
}
}
数据模块:
public partial class MainForm : Form
{
private readonly DataModule _dataModule = DataModule.GetInstance();
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = _dataModule.AppDataSet;
dataGridView1.DataMember = "MESSAGEQUEUE";
_dataModule.LoadMessageQueueDataTable();
}
}
private void FirebirdRemoteEventOnRemoteEventCounts(object sender, FbRemoteEventEventArgs fbRemoteEventEventArgs)
{
switch (fbRemoteEventEventArgs.Name.Trim().ToUpper())
{
case "QUEUE_NEW_MESSAGE":
if (fbRemoteEventEventArgs.Counts > 0)
{
LoadMessageQueueDataTable();
}
break;
}
}
public void LoadMessageQueueDataTable()
{
if (ConnectToFirebird())
{
using (var firebirdTransaction = FirebirdConnection.BeginTransaction())
{
using (var firebirdCommand = new FbCommand
{
Connection = firebirdTransaction.Connection,
Transaction = firebirdTransaction,
CommandType = CommandType.Text,
CommandText = "select MESSAGEQUEUEID, CREATEDATETIME, SENDER, RECIPIENT, TEXT from MESSAGEQUEUE"
})
{
AppDataSet.Tables["MESSAGEQUEUE"].Clear();
try
{
AppDataSet.Tables["MESSAGEQUEUE"].Load(firebirdCommand.ExecuteReader());
firebirdCommand.Transaction.Commit();
}
catch (FbException firebirdException)
{
firebirdCommand.Transaction.Rollback();
}
}
}
}
}
错误:
在DataModule类中添加和更改FbRemoteEvent处理程序:
public delegate void DelegateMessageQueueTableUpdate();
public event DelegateMessageQueueTableUpdate MessageQueueTableUpdate;
private void FirebirdRemoteEventOnRemoteEventCounts(object sender, FbRemoteEventEventArgs fbRemoteEventEventArgs)
{
switch (fbRemoteEventEventArgs.Name.Trim().ToUpper())
{
case "QUEUE_NEW_MESSAGE":
if (fbRemoteEventEventArgs.Counts > 0)
{
if (MessageQueueTableUpdate != null)
{
MessageQueueTableUpdate();
}
}
break;
}
}
主要形式:
public partial class MainForm : Form
{
private readonly DataModule _dataModule = DataModule.GetInstance();
private delegate void RefreshMessageQueueTable();
public MainForm()
{
InitializeComponent();
_dataModule.MessageQueueTableUpdate += () =>
{
if (dataGridView1.InvokeRequired)
{
dataGridView1.Invoke(new RefreshMessageQueueTable(_dataModule.LoadMessageQueueDataTable));
}
else
{
_dataModule.LoadMessageQueueDataTable();
}
};
}
private void MainForm_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = _dataModule.AppDataSet;
dataGridView1.DataMember = "MESSAGEQUEUE";
_dataModule.LoadMessageQueueDataTable();
}
}
你能改为显示英文错误信息吗。另外,您是在事件线程上还是在其他线程上填充数据集?@DataGridView中的MarkRotterVeel异常:System.IndexOutOfRangeException:未设置索引0。在System.Windows.Forms.CurrencyManager.get_项(Int32索引)中,在System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex)中若要替换此默认窗口,请处理事件DataError。@MarkRotVeel捕获FBRemoteEvent时,我不会创建另一个线程来加载datatable。据我所知,Firebird事件是在另一个线程上处理的(虽然不是100%确定)@MarkRotVeel如果我们直接在可视组件中更新数据,我认为可以使用委托。但数据集不是可视组件,也没有InvokeRequired属性。。。最奇怪的是,一个类似的项目在成功的工作,但我找不到这部分项目之间的差异。