Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button to a column in the DataGridView

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Software Title", typeof(string)));
dt.Columns.Add(new DataColumn("Version", typeof(string)));
dt.Columns.Add(new DataColumn("Uninstall", typeof(System.Windows.Forms.Button)));

DataRow dr = dt.NewRow();
dr[0] = "App";
dr[1] = "1.0";
Button uninstall = new Button();
uninstall.Text = "Uninstall";

dr[2] = uninstall;

dt.Rows.Add(dr);

dataGridViewSoftware.DataSource = dt;

The text appears but button never shows up.

like image 802
software is fun Avatar asked Jan 17 '14 17:01

software is fun


People also ask

How to add Button to DataGridView?

Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn to your DataGridView - Not directly to the DataTable . This should occur somewhere after you bind the DataTable to the DataGridView . Show activity on this post. Show activity on this post.

How to add Button in DataGridView Cell in c#?

All you need to do is to add a DataGridViewButtonCell where you want buttons, and DataGridViewTextBoxCell where you do not. The column has to be DataGridViewButton type. Show activity on this post. From what I've seen in the links it shows the entire column havint the same type.

Which class can you use to add a button column to a DataGridView control?

DataGridViewButtonColumn Class (System.

How do you put a button on a grid?

To add these ButtonFields, click on the Edit Columns link from the GridView's smart tag, select the ButtonField field type from the list in the upper left and click the Add button. Move the two ButtonFields so that they appear as the first two GridView fields.


3 Answers

Assuming you are in Windows Forms, you need to add a DataGridViewButtonColumn to your DataGridView - Not directly to the DataTable.

This should occur somewhere after you bind the DataTable to the DataGridView.

Something like this should work:

DataGridViewButtonColumn uninstallButtonColumn = new DataGridViewButtonColumn();
uninstallButtonColumn.Name = "uninstall_column";
uninstallButtonColumn.Text = "Uninstall";
int columnIndex = 2;
if (dataGridViewSoftware.Columns["uninstall_column"] == null)
{
    dataGridViewSoftware.Columns.Insert(columnIndex, uninstallButtonColumn);
}

Of course you will have to handle the CellClick event of the grid to do anything with the button.

Add this somewhere in your DataGridView Initialization code

dataGridViewSoftware.CellClick += dataGridViewSoftware_CellClick;

Then create the handler:

private void dataGridViewSoftware_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridViewSoftware.Columns["uninstall_column"].Index)
    {
        //Do something with your button.
    }
}
like image 90
Evan L Avatar answered Oct 04 '22 12:10

Evan L


Make it simple.

DataGridViewButtonColumn button = new DataGridViewButtonColumn();
{
    button.Name = "button";
    button.HeaderText = "Button";
    button.Text = "Button";
    button.UseColumnTextForButtonValue = true; //dont forget this line
    this.dataGridView1.Columns.Add(button);
}
like image 27
Ramgy Borja Avatar answered Oct 04 '22 12:10

Ramgy Borja


You can use the best method using below

                gvEmployees.AutoGenerateColumns = false;
                gvEmployees.ColumnCount = 4;


                DataGridViewButtonColumn SelectButton = new DataGridViewButtonColumn();
                SelectButton.Name = "Select";
                SelectButton.Text = "Select";
                SelectButton.UseColumnTextForButtonValue = true;
                if (gvEmployees.Columns["Select"] == null)
                {
                    gvEmployees.Columns.Insert(0, SelectButton);
                }

                DataGridViewButtonColumn DeleteButton = new DataGridViewButtonColumn();
                DeleteButton.Name = "Delete";
                DeleteButton.Text = "Delete";
                DeleteButton.UseColumnTextForButtonValue = true;
                if (gvEmployees.Columns["Delete"] == null)
                {
                    gvEmployees.Columns.Insert(1, DeleteButton);
                }

                gvEmployees.Columns[2].Name = "EmployeeID";
                gvEmployees.Columns[2].HeaderText = "EmployeeID";
                gvEmployees.Columns[2].DataPropertyName = "EmployeeID";
like image 25
Abhilash Thomas Avatar answered Oct 04 '22 14:10

Abhilash Thomas