C# 使用以编程方式添加的按钮刷新RadGridView:集合中已存在同名的列

C# 使用以编程方式添加的按钮刷新RadGridView:集合中已存在同名的列,c#,winforms,telerik,C#,Winforms,Telerik,使用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 col

使用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的重新绑定等。是否有其他方法完全清除列?(我还尝试在网格中循环并删除所有列,但仍然得到相同的错误


还有更好的方法可以做到这一点,而不必重新格式化和重新绘制控件?

为了确保清除网格中的列,还可以调用以下方法:

radGridView1.Columns.Clear();

非常感谢你把我逼疯了,不要做太多winforms或Telerik的工作。