使用以编程方式添加的按钮刷新 RadGridView:集合中已存在同名列 [英] Refreshing RadGridView with Programmatically added Button: A column with the same Name already exists in the collection

查看:17
本文介绍了使用以编程方式添加的按钮刷新 RadGridView:集合中已存在同名列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 GridView 添加文档指针时遇到问题.当我刷新网格时,尝试添加(或重新添加)两个以编程方式添加的按钮列时出错.

Running into an issue using a GridView to Add Document pointers. When I refresh the grid I get an error trying to add (or re-add) two programmatically added button columns.

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

对我来说可能是一个愚蠢的疏忽,但我开始变得有点疯狂!

Might be a dumb oversight on my part but I'm starting to go a little nutty !

我有以下代码:

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

-- 添加两个按钮供用户更新行或查看文档

-- Add two buttons for user to update row or view document

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()); }
}

---- 处理按钮事件

---- Handle Button Events

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);}
        }
}

--加载文档网格

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();
}

所以基本上网格加载并按预期执行,直到出现错误时添加新文档:

So basically the grid loads and performs as expected up until adding a new document when I get an error:

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.是否有另一种方法可以完全清除列?(我也尝试遍历网格并删除所有列,但仍然出现相同的错误.

Saying that the button controls are all ready in the collection. I have tried to refresh rebind etc the GridView. Is there another method to completely clear the columns? (I also tried to loop through the grid and remove all columns and still get the same error.

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

Even Better is there a way to do this without having to reformat and repaint the control unnecessarily?

推荐答案

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

To make sure you clear the column in the grid, you can call the following method as well:

radGridView1.Columns.Clear();

这篇关于使用以编程方式添加的按钮刷新 RadGridView:集合中已存在同名列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆