2014-05-22 101 views
0

我有一個MainForm(帶有dataGridView)和DataModule類,並帶有自己的LoadDataTable方法,該方法執行FbCommand選擇sql並填充數據集中的數據表和FbRemoteEvent catch方法。當我運行我的應用程序時,在MainForm_OnLoad中調用LoadDataTable方法,並且我的dataGridView顯示數據成功。但是當我的應用程序從服務器捕獲FbRemoteEvent並調用LoadDataTable方法時,dataGridView中發生異常。爲什麼?在FbRemoteEvent上填充DataGridView

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

錯誤: enter image description here

+0

你能顯示的英文錯誤訊息。你是否也在事件線程或不同的線程上填充數據集? –

+0

DataGridView中的@MarkRotteveel異常: System.IndexOutOfRangeException:未設置索引0。 in System.Windows.Forms.CurrencyManager.get_Item(Int32 index) in System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowIndex) 要替換此默認窗口,請處理事件DataError。 –

+0

@MarkRotteveel我不會創建另一個線程來加載數據表時捕獲FbRemoteEvent –

回答

1

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

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