-2

I have a c# DataGridView with some columns. I want to add an Action Column as the last Cell in each Row.

There should be buttons or icons like View,Delete,Update in each Row.

TaW
  • 53,122
  • 8
  • 69
  • 111
M Imtiaz
  • 97
  • 1
  • 2
  • 6
  • You would have to add a column of type `DataGridViewButtonColumn` for __each__ command..Many examples out there and in here.. – TaW Jun 13 '15 at 11:10
  • possible duplicate of [How to add a button to a column in the DataGridView](http://stackoverflow.com/questions/21191950/how-to-add-a-button-to-a-column-in-the-datagridview) – TaW Jun 13 '15 at 11:14
  • Thanks but i think my problem is a little different, i need to add multiple buttons i.e a panel of buttons( Like Update,Delete,Edit,View) how can i add a panel of buttons each having a different actions. – M Imtiaz Jun 13 '15 at 13:00
  • No, you can't do that easily. You just can't add __controls__ to a cell. You can start [here](http://stackoverflow.com/questions/8955144/how-to-place-a-custom-control-within-a-cells-of-datagridview-in-window-form) or [here](https://msdn.microsoft.com/en-us/library/7tas5c80.aspx?PHPSESSID=o1fb21liejulfgrptbmi9dec92) to create a __custom cell type__, but be prepared to do a lot of work for a few measerly Buttons! Clearly not worth it imo.. Add a Column for each Button and your good! – – TaW Jun 13 '15 at 14:49
  • Here is a quote: _The DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing. To do this, you must define classes that derive from DataGridViewColumn and DataGridViewCell. You must also define a class that derives from Control and implements the IDataGridViewEditingControl interface._ – TaW Jun 13 '15 at 14:52
  • Did you resolve your problem? – TaW Jun 23 '15 at 06:01

1 Answers1

0

Here are a few options I can think of:

  • By far the simplest is to add one Column for each Button. (Recommended!)
  • Of you could create a custom cell type, maybe a Panel subclass with the Buttons you want. This involves implementing IDataGridViewEditingControl iterface with at least a dozen fields and methods and also two custom classes derived from DataGridViewColumn and DataGridViewCell with many more things to do. In short a huge amount of work! For a sophisticated editing control maybe worth it. For a few Buttons certainly not! (Not recommended!)
  • Or you could fake the Buttons by a little owner-drawing magic in the CellPainting event. See below..!
  • Or you could add one sopisticated control outside the DataGridView that acts on the current row. The usual way!

Here is a fun little example, that owner-draws the four 'SIDU' commands in the 4th Column of a DatagGridView DGV:

enter image description here

private void Form1_Load(object sender, EventArgs e)
{
    DGV.Rows.Add(12);
    for( int i = 0; i< DGV.Rows.Count; i++)
    {
        DGV[0, i].Value = i;
        DGV[1, i].Value = R.Next(1000);
        DGV[2, i].Value = rights[R.Next(rights.Count)];
        DGV[3, i].ReadOnly = true;
    }
}

List<string> rights = new List<string> 
  { "SIDU", "SID-", "SI-U", "S-DU", "SI--", "S--U", "S-D-", "S---" };
Dictionary<char, string> rightsTexts = new Dictionary<char, string> 
  { { 'S', "Select" }, { 'I', "Insert" }, { 'D', "Delete" }, { 'U', "Update" } };
Random R = new Random(1);


private void DGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.ColumnIndex == 3 && e.RowIndex >= 0)
    {
        string r = DGV[2,e.RowIndex].Value.ToString();
        StringFormat format = new StringFormat()
        { LineAlignment = StringAlignment.Center,  Alignment = StringAlignment.Center};

        int w = e.CellBounds.Width / 4;
        int y = e.CellBounds.Y + 1;
        int h = e.CellBounds.Height - 2;

        e.PaintBackground(e.CellBounds, false);

        for (int i = 0; i < 4; i++)
        {
           int x = e.CellBounds.X + i * w;
           Rectangle rect = new Rectangle(x, y, w, h);
           ControlPaint.DrawButton(e.Graphics, rect, ButtonState.Normal);
           if (rightsTexts.ContainsKey(r[i])) 
               e.Graphics.DrawString(rightsTexts[r[i]], DGV.Font,
                                     SystemBrushes.WindowText, rect ,format );
        }
        e.Handled = true;
    }
}

private void DGV_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex == 3 && e.RowIndex >= 0)
    {
        DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];
        int w = cell.Size.Width;
        int buttonIndex = e.X * 4 / w;
        Text = rightsTexts.ElementAt(buttonIndex).Value;
    }
}

The drawing stuff was thrown up rather q&d, so you could invest a lot more care to fine-tune it..

I have chosen to display the rights in a visible cell for demostration. For production the action cell's value would be the obvious place.

TaW
  • 53,122
  • 8
  • 69
  • 111