1teamsah
1teamsah

Reputation: 1933

How to add button to any datagridview column's last cell

I want to add button in last cell of any columns. This is not working. How can I do for this?

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewButtonCell button = (row.Cells["1"] as DataGridViewButtonCell);
}

EDIT: I don't want to add column. I want to add these button on any columns' last cell, not row's.

enter image description here

Upvotes: 1

Views: 3276

Answers (3)

TaW
TaW

Reputation: 54433

You have to create a DataGridViewButtonCell for each Column you want to adorn:

DataGridViewButtonCell cell_1 = new DataGridViewButtonCell();

Then you can replace the original Cells with the DataGridViewButtonCells:

dataGridView1[oneOfYourButtonColumnIndices , buttonRowIndex] = cell_1;

Note that you need to create seperate Cells for each Button! Each can only be added once!

You can style the Cell as you like:

cell_1.Value = "Conquer";
cell_1.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

Hoever you can't register any events with it as the type DataGridViewCell is not a Control and does not implement an event model.

Instead all events must be handled in the DataGridView. Here is an example:

List<int> YourButtonColumnIndices = new List<int> {3,5,7};

private void DGV_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];

    // check if we have hit a button cell..
    if (YourButtonColumnIndices.Contains(e.ColumnIndex) && cell is DataGridViewButtonCell )
    {
        Console.WriteLine(cell.Value.ToString());
        // do things that depend on the button:
        // if (cell.Value == "Conquer")  doConquer(..);
        // else (cell.Value == "Command")  doCommand(..);
        // ..
    }
}

You can see that I can access the cell's Value just like normal. It is also displayed as the Button's Text.

Note that all Cells have a Tag property and you could store anything you like in there, including a delegate!

Also note that in your model the buttons' row may well change when you insert or delete rows. Therefore you should take care to either refer to the buttonRowIndex only when you add the buttons or keep it up to date. I didn't event check for it in the Click event as the column index plus the cell type should be enough. If they are not, you can also check on the values..

Of course you can code the doCommand methods as you like, adding parameters as needed..

Btw: Regular buttons are always Silver or whatever your system has for buttons in its color scheme. But you can make them FlatStyle buttons and voila, the Cell's Backcolor (and/or Forecolor) will be used:

 cell_1.FlatStyle = FlatStyle.Flat;
 cell_1.Style.BackColor = Color.LightSkyBlue;

enter image description here

If you have more than two or three such buttons you may want to create a Dictionary to manage them, maybe like this:

Dictionary<int, string> buttonList = new Dictionary<int, string>();
buttonList.Add(3, "Save Operator");
buttonList.Add(5, "Save An.1");
buttonList.Add(7, "Save An.2");

and use it to create them:

foreach(int i in buttonList.Keys)
  {
     DataGridViewButtonCell cell= new DataGridViewButtonCell();
     cell.Value = buttonList[i];
     cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
     dataGridView1[i, buttonRowIndex] = cell;
  }

and to check for them in the CellClick:

if (buttonList.Keys.Contains(e.ColumnIndex) && cell is DataGridViewButtonCell )
{  
  if (e.ColumnIndex] == 3) doStuff3()
  else if (e.ColumnIndex] == 5) doStuff5()
  //..

Upvotes: 4

nickson104
nickson104

Reputation: 580

You could employ a template field instead of a bound field. This would enable you to place a footer inside the field.

<asp:TemplateField>
       <ItemTemplate>
             <asp:Button ID="btnEdit" CommandName="Edit" Text="Edit" runat="server" CommandArgument='<%# Container.DataItemIndex %>' />
       </ItemTemplate>
       <FooterTemplate>
         <asp:Button ID="btnNew" CommandName="New" Text="New" runat="server" CommandArgument='<%# Container.DataItemIndex %>' />
       </FooterTemplate>                 
</asp:TemplateField>

And if I remember correctly, this is then handled in the rowcommand event (therefore the command name and arguement).

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16956

Update:

As question is now updated and the requirement is to add button to the cell not the button column entirely.

You can use DataGridViewButtonCell

var button = new DataGridViewButtonCell(); 

in case, if you need a button column below code will help.

DataGridViewButtonColumn is what you need.

DataGridViewButtonColumn button = new DataGridViewButtonColumn();
    button.Name = "WhatEver";
    button.Text = "ClickMe";

dataGridView1.Columns.Add(button);

Upvotes: 0

Related Questions