2

I want to add a custom DataGridViewColumn to my DataGridView. This column should create the following cell per row

img

At first I created a custom UserControl that creates a label with a button.

    private class AllocationControl : UserControl
    {
        public AllocationControl(IndexField[] indexFields, BatchField[] batchFields)
        {
            Label lbl = new Label();
            Controls.Add(lbl);

            ContextMenuStrip contextMenu = new ContextMenuStrip();
            // fill the menu
            Controls.Add(contextMenu);

            Button btn = new Button();
            btn.Click += (object sender, EventArgs e) =>
            {
                contextMenu.Show(Cursor.Position);
            };
            Controls.Add(btn);
        }

        public string DisplayedName { get; private set; }
        public double SelectedID { get; private set; }
    }

I have to pass in some data as constructor parameters but this is not relevant for the question.

After that I create a custom DataGridViewCell

    private class DataGridViewAllocationCell : DataGridViewCell
    {
        public DataGridViewAllocationCell()
        {
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            AllocationControl allocationControl = value as AllocationControl;
            Bitmap allocationControlImage = new Bitmap(cellBounds.Width, cellBounds.Height);
            allocationControl.DrawToBitmap(allocationControlImage, new Rectangle(0, 0, allocationControl.Width, allocationControl.Height));
            graphics.DrawImage(allocationControlImage, cellBounds.Location);
        }
    }

This cell should keep the custom control and display it.

At the end I add this cell to my custom DataGridViewColumn by setting the CellTemplate

    private class DataGridViewAllocationColumn : DataGridViewColumn
    {
        public DataGridViewAllocationColumn()
        {
            CellTemplate = new DataGridViewAllocationCell();
        }
    }

My question is how can I assign the UserControl to the DataGridViewCell?

I took this guide

https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-host-controls-in-windows-forms-datagridview-cells

but they all show up how to create a single control and put it into a cell. I have to setup three controls (label, button and contextMenu) within one cell.

  • You really need to use DataGridView? You can use other Containers for that, it would be easier – Pancabiel Nov 08 '18 at 11:24
  • yes, I need to display it in a `DataGridViewColumn` –  Nov 08 '18 at 11:44
  • 1
    Isn't an `UserControl` type of a `Control`? whats the difference for you? – S.Serpooshan Nov 12 '18 at 08:12
  • You always can make three columns looks like one... – Fabio Nov 12 '18 at 08:18
  • Well, the guide from the link shows what you need to do. Your `AllocationControl` *is a* single control, what is inside it (the inner controls) doesn't matter from the `DataGridView` point of view. – Ivan Stoev Nov 12 '18 at 08:49
  • A control can be hosted in cell just in edit mode. To show a control on all rows, you can use either of the following options: **(1)** You can customize paint of cells and draw the control over cells. **(2)** You can position instances of the control on top of cells manually. – Reza Aghaei Nov 13 '18 at 01:01
  • In case that you like to continue the way you are drawing the control over cells, you can take a look at [this post](https://stackoverflow.com/q/10920555/3110834). – Reza Aghaei Nov 13 '18 at 01:17

2 Answers2

1

There are 3 main pillars for a new column type:

  • DataGridViewColumn is responsible for properties which you set in design mode in column editor of the control.
  • DataGridViewCell is responsible for appearance of the cell. It renders the value and other painting parts in cell and initialize the editing control (if the column has any editing control).
  • DataGridViewEditingControl is responsible for editing the value of the cell.

When creating a new column you can derive from DataGridViewColumn or one of its derived classes. Also when creating a new cell for the column, you can derive from DataGridViewCell or one of its derived classes. Also when creating a new editing control, you can derive from one of the existing editing controls, or you can start by deriving from a control class and implementing IDataGridViewEditingControl interface.

Keep in mind, the editing control will be shown just in the editing cell. Rest of cells show what you render in the paint method of the cell.

Example

Here in this post, I've shared an example of drawing a custom cell containing a label and a button. I've not created an editing control because for this example, we can derive from DataGridViewButtonColumn and DataGridViewButtonCell without needing to create an editing control. We just add some properties to column and change paint logic and override OnContentClick to show the context menu, like this:

enter image description here

The custom column has LabelText and ButtonText properties. When you click on the button part, it shows the ContextMenuStrip which you assigned to corresponding property.

Note: This is just an example and depending on the requirements, you may want to change properties, rendering logic and the way that you show menu or anything else. But I think it's a good start point.

Here is the code:

using System.Drawing;
using System.Windows.Forms;
public class DataGridViewAllocationControlColumn : DataGridViewButtonColumn
{
    public DataGridViewAllocationControlColumn()
    {
        this.CellTemplate = new DataGridViewAllocationControlCell();
    }
    public string LabelText { get; set; }
    public string ButtonText { get; set; }
    public override object Clone()
    {
        var c = (DataGridViewAllocationControlColumn)base.Clone();
        c.LabelText = this.LabelText;
        c.ButtonText = this.ButtonText;
        return c;
    }
}
public class DataGridViewAllocationControlCell : DataGridViewButtonCell
{
    protected override void Paint(Graphics graphics, Rectangle clipBounds,
        Rectangle cellBounds, int rowIndex, 
        DataGridViewElementStates elementState,
        object value, object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        var g = this.DataGridView;
        var c = (DataGridViewAllocationControlColumn)this.OwningColumn;
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState,
            value, formattedValue, errorText, cellStyle, advancedBorderStyle,
            DataGridViewPaintParts.All &
            ~DataGridViewPaintParts.ContentBackground &
            ~DataGridViewPaintParts.ContentForeground);
        var r1 = g.GetCellDisplayRectangle(c.Index, rowIndex, false);
        var r2 = GetContentBounds(rowIndex);
        var r3 = new Rectangle(r1.Location, new Size(GetLabelWidth(), r1.Height));
        r2.Offset(r1.Location);
        base.Paint(graphics, clipBounds, r2, rowIndex, elementState,
            value, c.ButtonText, errorText, cellStyle, advancedBorderStyle,
            DataGridViewPaintParts.All);
        TextRenderer.DrawText(graphics, c.LabelText, cellStyle.Font,
            r3, cellStyle.ForeColor);
    }
    protected override Rectangle GetContentBounds(Graphics graphics, 
        DataGridViewCellStyle cellStyle, int rowIndex)
    {
        var w = GetLabelWidth();
        var r = base.GetContentBounds(graphics, cellStyle, rowIndex);
        return new Rectangle(r.Left + w, r.Top, r.Width - w, r.Height);
    }
    protected override void OnContentClick(DataGridViewCellEventArgs e)
    {
        base.OnContentClick(e);
        var g = this.DataGridView;
        var c = (DataGridViewAllocationControlColumn)this.OwningColumn;
        var r1 = GetContentBounds(e.RowIndex);
        var r2 = g.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
        var p = new Point(r2.Left + r1.Left, r2.Top + r1.Bottom);
        if (c.ContextMenuStrip != null)
            c.ContextMenuStrip.Show(g, p);
    }
    private int GetLabelWidth()
    {
        var c = (DataGridViewAllocationControlColumn)this.OwningColumn;
        var text = c.LabelText;
        return TextRenderer.MeasureText(text, c.DefaultCellStyle.Font).Width;
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • would you mind adding the following things: where do setup the context menu? If you setup the `LabelText` do you change it for all the cells within the column? The `LabelText` should represent the item I select from the context menu. –  Nov 13 '18 at 13:11
  • In this example, I set the label text using a property of column. It means it will be set for all cells. If it should respect to cell value or something, you can simply use `Value` property of cell, or add a custom property to the cell. – Reza Aghaei Nov 13 '18 at 13:16
  • I also edited the post to add a short introduction about main pillars of a custom column. – Reza Aghaei Nov 13 '18 at 13:17
0

I took this code and modified it to something a bit more dynamic this was a great start for me to build off of and I really appreciate this. I wanted to be able to select a folder with the button and open the folder by clicking the link.

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
using System.IO;

namespace Project
{

public class LinkButtonColumn : DataGridViewButtonColumn
{
    private EventHandler<DataGridViewCellMouseEventArgs> buttonClickHandler;
    private EventHandler<DataGridViewCellMouseEventArgs> linkClickHandler;
    public LinkButtonColumn()
    {
        this.CellTemplate = new DataGridViewLinkButtonCell();
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            base.CellTemplate = value;
            DataGridViewLinkButtonCell cell = CellTemplate as DataGridViewLinkButtonCell;
            if (cell != null)
                cell.ButtonClickHandler = ButtonClickHandler;
            cell.LinkClickHandler = LinkClickHandler;
            

        }
    }

    public EventHandler<DataGridViewCellMouseEventArgs> LinkClickHandler
    {
        get
        {
            return linkClickHandler;
        }
        set
        {
            DataGridViewLinkButtonCell cell = CellTemplate as DataGridViewLinkButtonCell;
            if (cell != null)
            {
                if (value != null)
                    cell.LinkClickHandler += value;
                else if (linkClickHandler != null)
                    cell.LinkClickHandler -= linkClickHandler;
            }
            linkClickHandler = value;
        }
    }

    public EventHandler<DataGridViewCellMouseEventArgs> ButtonClickHandler
    {
        get
        {
            return buttonClickHandler;
        }
        set
        {
            DataGridViewLinkButtonCell cell = CellTemplate as DataGridViewLinkButtonCell;
            if (cell != null)
            {
                if (value != null)
                    cell.ButtonClickHandler += value;
                else if (buttonClickHandler != null)
                    cell.ButtonClickHandler -= buttonClickHandler;
            }
            buttonClickHandler = value;
        }
    }

}
internal sealed class DataGridViewLinkButtonColumn : DataGridViewColumn
{
    private EventHandler<DataGridViewCellMouseEventArgs> buttonClickHandler;
    private EventHandler<DataGridViewCellMouseEventArgs> linkClickHandler;
    public string LabelText { get; set; }
    public string ButtonText { get; set; }
    public override object Clone()
    {
        var c = (DataGridViewLinkButtonColumn)base.Clone();
        c.LabelText = this.LabelText;
        c.ButtonText = this.ButtonText;
        return c;
    }
    public DataGridViewLinkButtonColumn() : base(new DataGridViewLinkButtonCell())
    {
    }

    public EventHandler<DataGridViewCellMouseEventArgs> LinkClickHandler
    {
        get
        {
            return linkClickHandler;
        }
        set
        {
            DataGridViewLinkButtonCell cell = CellTemplate as DataGridViewLinkButtonCell;
            if (cell != null)
            {
                if (value != null)
                    cell.LinkClickHandler += value;
                else if (linkClickHandler != null)
                    cell.LinkClickHandler -= linkClickHandler;
            }
            linkClickHandler = value;
        }
    }

    public EventHandler<DataGridViewCellMouseEventArgs> ButtonClickHandler
    {
        get
        {
            return buttonClickHandler;
        }
        set
        {
            DataGridViewLinkButtonCell cell = CellTemplate as DataGridViewLinkButtonCell;
            if (cell != null)
            {
                if (value != null)
                    cell.ButtonClickHandler += value;
                else if (buttonClickHandler != null)
                    cell.ButtonClickHandler -= buttonClickHandler;
            }
            buttonClickHandler = value;
        }
    }
}

public class DataGridViewLinkButtonCell : DataGridViewButtonCell
{
    private Rectangle clickRectangleValue = new Rectangle();
    private string buttonPosion = "Middle";
    public EventHandler<DataGridViewCellMouseEventArgs> ButtonClickHandler { get; set; }
    public EventHandler<DataGridViewCellMouseEventArgs> LinkClickHandler { get; set; }

    public Rectangle ClickRectangle
    {
        get
        {
            Rectangle newRect = new Rectangle();
            switch (buttonPosion)
            {
                case "Bottom":
                    newRect = new Rectangle(clickRectangleValue.X + clickRectangleValue.Width - 20, clickRectangleValue.Y + (clickRectangleValue.Height - 20), 20, 20);
                    break;
                case "Middle":
                    newRect = new Rectangle(clickRectangleValue.X + clickRectangleValue.Width - 20, clickRectangleValue.Y + ((clickRectangleValue.Height - 20) / 2), 20, 20);
                    break;
                case "Top":
                    newRect = new Rectangle(clickRectangleValue.X + clickRectangleValue.Width - 20, clickRectangleValue.Y, 20, 20);                        
                    break;
            }
            

            return newRect;
        }
    }

    public override object Clone()
    {
        DataGridViewLinkButtonCell cell = base.Clone() as DataGridViewLinkButtonCell;
        if (cell != null)
        {
            cell.ButtonClickHandler = ButtonClickHandler;
            cell.LinkClickHandler = LinkClickHandler;
        }
        return cell;
    }

    protected override void Paint(Graphics graphics, Rectangle clipBounds,
        Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates elementState,
        object value, object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        var g = this.DataGridView;
        var c = (DataGridViewLinkButtonColumn)this.OwningColumn;
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState,
            value, formattedValue, errorText, cellStyle, advancedBorderStyle,
            DataGridViewPaintParts.All &
            ~DataGridViewPaintParts.ContentBackground &
            ~DataGridViewPaintParts.ContentForeground);
        var r1 = g.GetCellDisplayRectangle(c.Index, rowIndex, false);
        

        
        Color forecolor = Color.Black;
        TextFormatFlags horizontal = TextFormatFlags.HorizontalCenter;
        TextFormatFlags vertical = TextFormatFlags.VerticalCenter;
        switch (cellStyle.Alignment)
        {
            case DataGridViewContentAlignment.BottomCenter:
                horizontal = TextFormatFlags.HorizontalCenter;
                vertical = TextFormatFlags.Bottom;
                break;
            case DataGridViewContentAlignment.BottomLeft:
                horizontal = TextFormatFlags.Left;
                vertical = TextFormatFlags.Bottom;
                break;
            case DataGridViewContentAlignment.BottomRight:
                horizontal = TextFormatFlags.Right;
                vertical = TextFormatFlags.Bottom;
                break;
            case DataGridViewContentAlignment.MiddleCenter:
                horizontal = TextFormatFlags.HorizontalCenter;
                vertical = TextFormatFlags.VerticalCenter;
                break;
            case DataGridViewContentAlignment.MiddleLeft:
                horizontal = TextFormatFlags.Left;
                vertical = TextFormatFlags.VerticalCenter;
                break;
            case DataGridViewContentAlignment.MiddleRight:
                horizontal = TextFormatFlags.Right;
                vertical = TextFormatFlags.VerticalCenter;
                break;
            case DataGridViewContentAlignment.TopCenter:
                horizontal = TextFormatFlags.HorizontalCenter;
                vertical = TextFormatFlags.Top;
                break;
            case DataGridViewContentAlignment.TopLeft:
                horizontal = TextFormatFlags.Left;
                vertical = TextFormatFlags.Top;
                break;
            case DataGridViewContentAlignment.TopRight:
                horizontal = TextFormatFlags.Right;
                vertical = TextFormatFlags.Top;
                break;
        }


        if (g.Rows[rowIndex].Cells[c.Index].Selected)
        {
            if (value != null)
            {
                if (Directory.Exists(value.ToString()) || File.Exists(value.ToString()))
                {
                    forecolor = Color.Red;
                    TextRenderer.DrawText(graphics, value.ToString(), new Font(cellStyle.Font, FontStyle.Underline),
                  r1, forecolor, Color.Empty,
                  horizontal |
                  vertical |
                  TextFormatFlags.TextBoxControl |
                  TextFormatFlags.WordBreak |
                  TextFormatFlags.EndEllipsis);
                }
                else
                {
                    forecolor = Color.Blue;
                    TextRenderer.DrawText(graphics, value.ToString(), new Font(cellStyle.Font, FontStyle.Regular),
                  r1, Color.White, Color.Empty,
                  horizontal |
                  vertical |
                  TextFormatFlags.TextBoxControl |
                  TextFormatFlags.WordBreak |
                  TextFormatFlags.EndEllipsis);
                }
            }
        }
        else
        {
            if (value != null)
            {
                if (Directory.Exists(value.ToString()) || File.Exists(value.ToString()))
                {
                    forecolor = Color.Blue;
                    TextRenderer.DrawText(graphics, value.ToString(), new Font(cellStyle.Font, FontStyle.Underline),
                  r1, forecolor, Color.Empty,
                  horizontal |
                  vertical |
                  TextFormatFlags.TextBoxControl |
                  TextFormatFlags.WordBreak |
                  TextFormatFlags.EndEllipsis);
                }
                else
                {
                    forecolor = Color.Blue;
                    TextRenderer.DrawText(graphics, value.ToString(), new Font(cellStyle.Font, FontStyle.Regular),
                  r1, Color.Black, Color.Empty, 
                  horizontal |
                  vertical |
                  TextFormatFlags.TextBoxControl |
                  TextFormatFlags.WordBreak |
                  TextFormatFlags.EndEllipsis);
                }
            }
        }

        

        Point cursorPosition = this.DataGridView.PointToClient(Cursor.Position);

        
        if (cellBounds.Contains(cursorPosition))
        {
            clickRectangleValue = cellBounds;
            Rectangle newRect = new Rectangle(cellBounds.X + 1,
                cellBounds.Y + 1, cellBounds.Width - 4,
                cellBounds.Height - 4);
            DataGridViewCellStyle style = cellStyle;
            style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            base.Paint(graphics, clipBounds, ClickRectangle, rowIndex, elementState,
            value, c.ButtonText, errorText, style, advancedBorderStyle,
            DataGridViewPaintParts.All);
        }
        
    }
    protected override Rectangle GetContentBounds(Graphics graphics,
        DataGridViewCellStyle cellStyle, int rowIndex)
    {
        
        var r = base.GetContentBounds(graphics, cellStyle, rowIndex);
        switch (cellStyle.Alignment)
        {
            case DataGridViewContentAlignment.BottomCenter:
                buttonPosion = "Bottom";
                break;
            case DataGridViewContentAlignment.BottomLeft:
                buttonPosion = "Bottom";
                break;
            case DataGridViewContentAlignment.BottomRight:
                buttonPosion = "Bottom";
                break;
            case DataGridViewContentAlignment.MiddleCenter:
                buttonPosion = "Middle";
                break;
            case DataGridViewContentAlignment.MiddleLeft:
                buttonPosion = "Middle";
                break;
            case DataGridViewContentAlignment.MiddleRight:
                buttonPosion = "Middle";
                break;
            case DataGridViewContentAlignment.TopCenter:
                buttonPosion = "Top";
                break;
            case DataGridViewContentAlignment.TopLeft:
                buttonPosion = "Top";
                break;
            case DataGridViewContentAlignment.TopRight:
                buttonPosion = "Top";
                break;
        }
        clickRectangleValue = r;
        
        return new Rectangle(ClickRectangle.X, ClickRectangle.Y, ClickRectangle.Width, ClickRectangle.Height);
    }
        

    protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
    {
        int x = e.X;
        int y = e.Y;

        Point cursorPosition = this.DataGridView.PointToClient(Cursor.Position);

        var g = this.DataGridView;
        var r1 = GetContentBounds(e.RowIndex);
        var r2 = g.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
        r1.X = r1.X + r2.X;
        r1.Y = r1.Y + r2.Y;
        if (r1.Contains(cursorPosition))
        {                
            if (ButtonClickHandler != null)
            {
                this.ButtonClickHandler.Invoke(this,e); 
            }
        }
        else
        {
            if (r2.Contains(cursorPosition))
            {
                if (LinkClickHandler != null)
                {
                    this.LinkClickHandler.Invoke(this, e);
                }
            }
        }
        
    }
    
    protected override void OnMouseEnter(int rowIndex)
    {
        Cursor.Current = Cursors.Hand;
        this.DataGridView.Cursor = Cursor.Current;
        this.DataGridView.InvalidateCell(this);
        
    }
    
    protected override void OnMouseLeave(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
        Cursor.Current = Cursors.Default;
        this.DataGridView.Cursor = Cursor.Current;
    }
}
}

Then I pragmatically add the column to the grid.

if (!Addendum_dataGridView.Columns.Contains("Folder_Location"))
    {
    DataGridViewLinkButtonColumn textButtonColumn = new DataGridViewLinkButtonColumn();

    textButtonColumn.ValueType = typeof(string);
    textButtonColumn.HeaderText = "Folder Location";
    textButtonColumn.ButtonText = "...";
    textButtonColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    textButtonColumn.DefaultCellStyle.Padding = new Padding(1, 0, 0, 0);
    textButtonColumn.Width = 100;
    textButtonColumn.Name = "Folder_Location";
    textButtonColumn.DataPropertyName = "Folder_Location";           
    textButtonColumn.LinkClickHandler = (o, e) =>
     {
     if(Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Value != DBNull.Value)
     { 
         if (Directory.Exists(Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Value.ToString()) || File.Exists(Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Value.ToString()))
         {
         Process.Start(Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Value.ToString());
         }
     }
     };
    textButtonColumn.ButtonClickHandler = (o, e) =>
    {
    CommonOpenFileDialog cofd = new CommonOpenFileDialog();
    cofd.IsFolderPicker = true;
    cofd.Multiselect = false;
    cofd.Title = "Select Addendum Folder";
    if (Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Value != DBNull.Value)
    {
        cofd.InitialDirectory = Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Value.ToString();
    }
    else
    {
        cofd.InitialDirectory = Directory.GetParent(Directory.GetParent(SQL_Commands.Get_Data("select top 1 Quote_Location from [Index].[Estimates_Project_info] where Project_ID = " + P_ID).FirstOrDefault()).ToString()).ToString();
    }
    if (cofd.ShowDialog() == CommonFileDialogResult.Ok)
    {
        if (Addendum_dataGridView.Rows[e.RowIndex].IsNewRow)
        {
            int r = Addendum_dataGridView.NewRowIndex;
            DataTable dt = Addendum_dataGridView.DataSource as DataTable;

            DataRow row = dt.NewRow();

            row["Addendum_Number"] = Path.GetFileNameWithoutExtension(cofd.FileName.ToString());
            row["Folder_Location"] = cofd.FileName.ToString();

            dt.Rows.Add(row);
            dt = Addendum_dataGridView.DataSource as DataTable;

            bool blank = true;
            DataGridViewRow dgvr = Addendum_dataGridView.Rows[Addendum_dataGridView.Rows.Count - 2];
            for (int i = 0; i < Addendum_dataGridView.ColumnCount;i++)
            {
                if (dgvr.Cells[i].Value != DBNull.Value)
                {
                    blank = false;
                }
        }

        if (blank)
        {
            Addendum_dataGridView.Rows.RemoveAt(Addendum_dataGridView.Rows.Count - 2);
        }

        insert_Addendum = true;
        Addendum_Data_Update(r);
    }
    else
    {
        Addendum_dataGridView["Folder_Location",e.RowIndex].Value = cofd.FileName.ToString();Addendum_dataGridView.Rows[e.RowIndex].Cells[Addendum_dataGridView.Columns["Folder_Location"].Index].Selected = true;
        Addendum_Data_Update(e.RowIndex);
    }
    }

    };
Addendum_dataGridView.Columns.Insert(Addendum_dataGridView.Columns.Count,textButtonColumn);
} 
Hack.Sign
  • 9
  • 2
  • this is a great answer, but I would look how old the question is, almost 2 years. Chances are, a solution has already been found :) – AntLaC Jul 31 '20 at 17:36
  • 1
    thanks I didn't even think about checking the date. I found this very helpful for what I was trying to accomplish so I thought that putting my code up may help someone else out in the future. – Hack.Sign Oct 23 '20 at 19:46