Refreshing RadGridView with Programmatically added Button: A column with the same Name already exists in the collection

StackOverflow https://stackoverflow.com/questions/23071318

  •  03-07-2023
  •  | 
  •  

Question

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 !

I have the following code:

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

--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 New Document

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

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?

Was it helpful?

Solution

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

radGridView1.Columns.Clear();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top