使用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);}
}
}
- 加载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
说集合中的按钮控件都已准备就绪。我试图刷新重新绑定等GridView。还有另一种方法可以完全清除列吗? (我也尝试遍历网格并删除所有列,但仍然得到相同的错误。
更好的是有没有办法在不必重新格式化和重新绘制控件的情况下执行此操作?
答案 0 :(得分:1)
要确保清除网格中的列,您还可以调用以下方法:
radGridView1.Columns.Clear();