2012-05-08 26 views
1

我得到一個datagridview默認錯誤對話框。那裏說它處理dataerror事件。 我從數據表中加載myy DGV中的數據。當我嘗試添加一個新行時,我用(*)單擊空行。當我開始在新的行單元格中鍵入一些值時,則會顯示數據錯誤並進入無限循環。DataError對話框

DataGridViewDataError.context = display. 
Error: Index 3 does not have a value. 

我的網格有3rows這裏的新行會索引3

下面是其中DGV加載代碼

private void frmPlant_Load(object sender, EventArgs e) 
    { 
     Program.fMain = this; 
     Program.LoadAllForms(); 
     string selectCommand = "select * from T1"; 
     FillData(selectCommand); 

    } 
    private void FillData(string selectCommand) 
    { 
     using (SQLiteConnection conn = new SQLiteConnection(connString)) 
     { 
      conn.Open(); 
      dataGridView1.AutoGenerateColumns = true; 

      da = new SQLiteDataAdapter("selectCommand", connString); 
      ds = new DataSet(); 

      commandBuilder = new SQLiteCommandBuilder(da); 

      da.Fill(ds, "T1"); 
      dt = ds.Tables[0]; 
      dataGridView1.DataSource = ds.Tables[0]; 
      dataGridView1.DataMember = "T1"; 
      dataGridView1.Columns["TID"].ReadOnly = true; 
     } 

    } 

我不知道確切位置的代碼這發生了。處理DataError事件。不知道UserAddedRows,RowsValidating將是有益的

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) 
     { 
      MessageBox.Show(e.Context.ToString()); 
      e.Cancel = true; 
     } 

謝謝 孫

+0

如果您爲每列定義的數據類型不匹配,則會發生這些情況。確保它! – nawfal

回答

0

不知道。嘗試用System.Diagnostics.Debug.WriteLine替換MessageBox.Show語句,並在RowsValidating事件中執行相同的操作。在這種情況下,MessageBox可能會導致問題,因爲它會「暫停」處理。

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) 
{ 
    System.Diagnostics.Debug.WriteLine(e.Context.ToString()); 
    e.Cancel = true; 
} 
+0

System.Diagnostics.Debug.WriteLine將輸出寫入哪裏? – user575219

+0

Visual Studio輸出窗口;查看 - >輸出; Ctrl + W,O – volody

2

MSDN最高審計機關以下內容:DataGridViewDataErrorEventArgs的

的columnIndex和rowIndex Properties對象與該事件相關聯的通常表示其中發生數據錯誤的單元格。但是,當外部數據源發生錯誤時,數據源可能不會提供發生錯誤的列。在這種情況下,ColumnIndex屬性通常表示錯誤發生時當前單元格的列。

使用這些屬性可以找出發生錯誤的列。

+0

dataGridView1_DataError事件給出e.Rowindex = 3和e.colindex = -1 – user575219

+0

除了nawfal建議,我會嘗試不設置TID列爲只讀。做到這一點,看看RowIndex和ColumnIndex屬性的值是什麼。 – Daniel