Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a buttons to some (not all) of the columns in my DataGridView?

I'm trying to update this DataGridView object such that if a value == "bob" there will be a button in a column next to its name, otherwise I don't want any button to appear.

DataGridViewTextBoxColumn valueColumn = new DataGridViewTextBoxColumn();
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();

buttonColumn.ReadOnly = true;
buttonColumn.Visible = false;

this.dgv.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
valueColumn,
buttonColumn,
});

//elsewhere...

if(value == "bob")
{
    Button button = new Button()
    {
        Text = "null",
    };

    index = dgv.Rows.Add(value, button);
    DataGridViewButtonCell buttonCell = dgv.Rows[index].Cells[2] as DataGridViewButtonCell;
    buttonCell.Visible = true;
}
else
{
    dgv.Rows.Add(value);
}

But, since I can't set Visible on a cell, this doesn't work. Is there a way to add a button to only the rows were Value == "bob"?

like image 965
user467384 Avatar asked Feb 26 '23 18:02

user467384


2 Answers

Here is a neat little hack that I've used before to accomplish this:

Instead of using a DataGridViewButtonColumn, use the DataGridViewTextBoxColumn and add a DataGridViewButtonCell where appropriate.

e.g.

    private void button1_Click(object sender, EventArgs e)
    {
        // Iterate through each of the rows.
        for (int i = 0; i < dgv.RowCount - 1; i++)
        {
            if (dgv.Rows[i].Cells[0].Value.ToString() == "bob")
            {
                // Here is the trick.
                var btnCell = new DataGridViewButtonCell();
                dgv.Rows[i].Cells[1] = btnCell;
            }
        }
    }

In the example above, I have two DataGridViewTextBoxColumns and iterate through each of the rows on a button click event. I check the first column to see if it contains "bob" and if it does, I add a button in the column next to it. You can use this trick however you want (i.e. button clicks, RowsAdded event, CellEndEdit event, etc.). Experiment in different ways. Hope this helps someone!

like image 170
VineAndBranches Avatar answered Feb 28 '23 08:02

VineAndBranches


There are two possibilities here, one ugly and one from MSDN.

The Ugly: Add a button to your DGV at runtime

Do the following:
- Add an unbound DataGridViewTextBoxColumn to your DGV. Note it's index value in your DGV; this is where you'll put your button.
- Use your DGV's CellFormatting event like so:

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
        if (e.ColumnIndex == 0) {  // Assumes column 0 has the data that determines if a button should be displayed.
            if (e.Value.ToString() == "bob") {  // Test if a button should be displayed on row.
                // Create a Button and add it to our DGV.
                Button cellButton = new Button();
                // Do something to identify which row's button was clicked.  Here I'm just storing the row index.
                cellButton.Tag = e.RowIndex;
                cellButton.Text = "Hello bob";
                cellButton.Click += new EventHandler(cellButton_Click);
                dataGridView1.Controls.Add(cellButton);
                // Your ugly button column is shown here as having an index value of 3.
                Rectangle cell = this.dataGridView1.GetCellDisplayRectangle(3, e.RowIndex, true);
                cellButton.Location = cell.Location;
            }
        }
    }

When a user clicks the button the cellButton_Click event will fire. Here's some test code:

    void cellButton_Click(object sender, EventArgs e) {
        Console.WriteLine("Hello from row: {0}", ((Button) sender).Tag);
    }

As you can see this isn't very refined. I based it on an even uglier sample I found. I'm sure you can modify it to suit your needs.

From MSDN: Roll your own (extend) DataGridViewButtonColumn that conditionally displays a disabled button.

For this option see How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control

Of course this option doesn't actually remove any buttons, only conditionally disables them. For your application however, this might be better.

like image 35
Jay Riggs Avatar answered Feb 28 '23 07:02

Jay Riggs