2014-04-14 23 views
0

遇到使用GridView添加文檔指針的問題。當我刷新網格時,嘗試添加(或重新添加)兩個以編程方式添加的按鈕列時出現錯誤。使用編程添加按鈕刷新RadGridView:集合中已經存在一列具有相同名稱的列

An unhandled exception of type 'System.InvalidOperationException' 
occurred in  Telerik.WinControls.GridView.dll 

Additional information: A column with the same Name 
already exists in the collection 

對我來說可能是一個愚蠢的忽略,但我開始有點瘋狂了!

我有以下代碼:

public GridEvents() {   
     gvDocs.RowFormatting += RowFormatting; 
     gvDocs.DataBindingComplete += GvDocsDataBindingComplete; 
     gvDocs.CommandCellClick += GvDocsCommandCellClick; 
} 

- 添加兩個按鈕爲用戶更新行或查看文檔

void GvDocsDataBindingComplete(object sender, GridViewBindingCompleteEventArgs e) { 
     //if (IsLoad == false) { return;} //Trying to exclude from second load doesn't work 
     try { 
      var subRow = new GridViewCommandColumn("SAVE", "SAVE") { Width = 40, HeaderText = "", MinWidth = 40, TextAlignment = ContentAlignment.MiddleCenter, UseDefaultText = true, DefaultText = Resources.Save }; 
      gvDocs.Columns.Add(subRow); //***!!!ERROR HERE!!!*** 
      var opnRow = new GridViewCommandColumn("VIEW", "VIEW") { Width = 40, HeaderText = "", MinWidth = 40, TextAlignment = ContentAlignment.MiddleCenter, UseDefaultText = true, DefaultText = Resources.View }; 
      gvDocs.Columns.Add(opnRow); 
     } 
     catch (Exception ex) { MessageBox.Show(ex.ToString()); } 
} 

----處理按鈕事件

void GvDocsCommandCellClick(object sender, EventArgs e){ 
     var col = gvDocs.CurrentColumn.Index; 
     if (col == 8) { UpdateDocument(); } 
     if (col == 9) { 
      try { Process.Start(gvDocs.CurrentRow.Cells[6].Value.ToString()); } 
      catch(Exception ex) { MessageBox.Show(Resources.FileDoesNotExist); Util.Log(ex);} 
     } 
} 

--Load DocGrid

public void LoadDocGrid() { 
     var doc = new SelectDocumentByOrder { ConnectionString = ConStr, fk_OrderID = Id }; 
     var ds = doc.ExecuteDataSet(); 
     gvDocs.DataSource = ds.Tables[0]; 
     FormatDocGrid(); //Set Color, Widths, Heights, DateFormatShort 
     gvDocs.DataSource = ds.Tables[0]; 
} 

- 增加新文檔

private void BtAddDocClick(object sender, EventArgs e) { 
     var dialog = new OpenFileDialog(); 
     var result = dialog.ShowDialog(); 
     if (result != DialogResult.OK) return; 
     var selectedFilePath = dialog.FileName; 
     var selectFile = Path.GetFileName(selectedFilePath); 
     var dir = Util.BuildFileSystem(txOrderNo.Text); 
     try { 
      if (File.Exists(dir + selectFile)) { 
       var name = Path.GetFileName(selectFile) + Util.GetRandomIntStr(); 
       var ext = Path.GetExtension(selectFile); 
       selectFile = name + ext; 
       File.Copy(selectedFilePath, dir + selectFile); 
      } 
      else { File.Copy(selectedFilePath, dir + selectFile); } 
     } 
     catch (Exception ex) { Log(ex); MessageBox.Show("Error Adding New Document";) } 
     InsertDocument(dir + selectFile); 
     //IsLoad = false; //Attmept to Exlude Button Add on reload Doesn't Work 
     //Reload Doc Grid HERE IS WHERE THE PROBLEM STARTS 
     gvDocs.DataSource = null; //Tried various things here to empty the control and reload, would rather just reload data without reformatting (repainting) the whole controls 
     gvDocs.Refresh(); 
     gvDocs.DataSource = null; 
     LoadDocGrid(); 
} 

所以基本上電網負荷和預期,直到添加新文檔時,我得到一個錯誤執行:

An unhandled exception of type 'System.InvalidOperationException' 
occurred in  Telerik.WinControls.GridView.dll 

Additional information: A column with the same Name 
already exists in the collection 

話說按鈕控件所有準備在收集。我試圖刷新rebind等GridView。是否有另一種方法來完全清除列? (我也通過網格試圖循環,並刪除所有列,仍然得到同樣的錯誤。

更好的是有沒有辦法做到這一點,而無需不必要的重新格式化並重新繪製控制?

謝謝你事先的任何見解

回答

1

爲了確保您清除網格中的列,您可以撥打下面的方法還有:

radGridView1.Columns.Clear(); 
+0

這工作非常感謝你這是逼我出的主意,不要做太多的winform或Telerik工作。 –

相關問題