简体   繁体   中英

Fill DataGridView on FbRemoteEvent

I have one MainForm (with dataGridView) and DataModule class with my own LoadDataTable method which execute FbCommand select sql and fill datatable in dataset and FbRemoteEvent catch method. When I run my app, in MainForm_OnLoad I call LoadDataTable method and my dataGridView show data successfully. But when my app catch FbRemoteEvent from server and call LoadDataTable method, exception occurred in dataGridView. Why?

MainForm:

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();
    }
}

DataModule:

    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();
                        }
                    }
                }
            }
        }

Error: 在此处输入图片说明

In DataModule class add and change FbRemoteEvent handler:

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;
            }
        }

In MainForm:

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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM