运行了一个问题,使用一个GridView添加文件指针。 当我刷新网格我得到一个错误试图添加(或重新添加)两大编程方式添加按钮列。
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];
}
--add新建文档
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
话说控制按钮都在收集准备。 我试图刷新重新绑定等GridView控件。 是否有其他方法彻底清除列? (我也通过网格试图循环,并删除所有列,仍然得到同样的错误。
更妙的是有没有办法做到这一点,而无需重新格式化,并重新绘制不必要的控制?